当调用 post 方法时,在服务器端获取空参数(PHP - Codeigniter)。当我使用JSON.stringify它时,它正在工作,但在服务器端,我必须使用有效负载处理请求($request_body = file_get_contents('php://input'))到目前为止还可以,问题是当我尝试上传文件(视频/图像)时,有效负载不支持.所以如果我使用表单数据,它会抛出网络错误,这意味着 Axios 甚至没有访问服务器。我已经从 javascript、php 和 postman 测试了相同的服务,所有这些都工作正常,只是反应本机调用失败了那又怎样?这是解决方案,我已经尝试了三天了,通过更改标题内容是一种不同的方式,但仍然无法理解。
下面给出以下代码
// alert('check ');
// const res = DocumentPicker.pick({
// type: [DocumentPicker.types.allFiles],
// }).catch(error => {
// alert('doct' + error);
// });
//Printing the log realted to the file
const imageUrl = 'https://image.shutterstock.com/image-photo/floral-spring-natural-landscape-wild-260nw-1274207287.jpg';
// if (true) {
// const fileToUpload = res;
const formData = new FormData();
formData.append('name', 'Image Upload');
formData.append('file_attachment', {
uri: imageUrl,
type: "image/jpg",
name: 'floral-spring-natural-landscape-wild-260nw-1274207287.jpg'
});
Axios({
url: URL + '/VideosController/upload',
method: 'POST',
data: formData,
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
},
}).then(
response => {
alert('res' + JSON.stringify(response.data));
},
error => {
alert('check+ ' + error);
})
.catch(error => {
alert('No error' + error);
});
当我使用 formdData 时出错:
错误=错误:EventTarget.handleError(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:155044:17)处出现网络错误(http://localhost:8081/index.bundle? platform=android&dev=true&minify=false:154946:16) 在 EventTarget.dispatchEvent (http://localhost:8081/index.bundle?
请解释实际发生的情况以及解决方案。
慕侠2389804
慕桂英3389331