当调用post方法时,在服务器端获取null参数(PHP-Codeigniter(。当我使用JSON.stringify
时,它正在工作,但在服务器端,我必须用有效载荷来处理请求($request_body = file_get_contents('php://input'))
目前还可以,问题是当我试图上传文件(视频/图像(时,有效载荷不支持。因此,如果我使用的是表单数据,它会引发网络错误,这意味着Axios甚至没有进入服务器。我已经测试了javascript、php和poster中的相同服务,它们都运行良好,只对本地调用失败做出反应。所以解决方案是什么?我已经尝试了3天,通过更改标头内容来解决这个问题,但仍然无法理解。
下面给出以下代码
// 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);
});
服务器端PHP代码
header("Access-Control-Allow-Methods: GET,POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
// By searching i seen it might cors error so added headers on top of my method no use
if(!empty($_FILES['file_attachment']['name'])) {
$res = array();
$name = 'file_attachment';
$imagePath = 'assets/upload/file_attachment';
$temp = explode(".",$_FILES['file_attachment']['name']);
$extension = end($temp);
$filenew = str_replace($_FILES['file_attachment']['name'],$name,$_FILES['file_attachment']['name']).'_'.time().''. "." .$extension;
$config['file_name'] = $filenew;
$config['upload_path'] = $imagePath;
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
print_r($_FILES);
exit;
$this->upload->set_filename($config['upload_path'],$filenew);
if(!$this->upload->do_upload('file_attachment')) {
$data = array('msg' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
if(!empty($data['file_name'])){
$res['url'] = 'assets/upload/file_attachment/'.$data['file_name'];
}
if (!empty($res)) {
echo json_encode(array("status" => 1, "data" => array() ,"msg" => 'upload successfully', 'base_url' => base_url(), "count" => '0'));
}else{
echo json_encode(array("status" => 1,"data" => array() ,"msg" => 'not found', 'base_url' => base_url(), "count" => '0'));
}
}
}
使用formdData时出错:
error=错误:createError处的网络错误(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:155044:17(。handleError(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:154946:16(在EventTarget.dispatchEvent(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:34135:27(在EventTarget.setReadyState(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33219:20(__didCompleteResponse(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33046:16(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:33156:47,位于RCTDeviceEventEmitter.emit(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:3422:37(__callFunction(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2750:44(http://localhost:8081/index.bundle?platform=android&dev=true&消息队列中的minify=false:2472:17__防护装置(http://localhost:8081/index.bundle?platform=android&dev=true&minify=false:2704:13(
请解释实际发生的事情以及解决方案。
首先需要提高服务器端的有效负载限制,但仍然会导致问题,然后提及细节。
最后,我通过将FLIPPER_VERSION升级到0.41.0找到了解决方案,这个问题得到了解决,但如果我升级了应用程序,甚至没有为我打开。所以现在我所做的是在下面的文件中,我只是对这些行进行了评论。为我工作。
\android\app\src\debug\java.com\appname\ReactNativeFlipper.java
public void apply(OkHttpClient.Builder builder) {
// builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
}
我刚刚评论了这条线以获得更多帮助,你可以查看下面的链接
网址:https://github.com/facebook/react-native/issues/28551#issuecomment-656601532
如果你想更新flipper版本,它将在..\android\gradle.properties