【发布时间】:2018-10-04 02:40:28
【问题描述】:
我想从我的 React-Native 移动应用程序将 Fetch POST 到我的后端 Laravel 服务器。我不知道缺少哪些部分或我做错了什么。这是 fetch :
fetch('http://example.com/react_test', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: inputName,
email: inputEmail,
phone: inputPhone
}),
}).then((response) => response.json())
.then((responseJson) => {
alert(JSON.stringify(responseJson))
}).catch((error) => {
alert(error);
});
我通过 POST 路由在后端接收数据
Route::post('/react_test', 'QuestionController@index');
public function index($request){
$n = $request->input('name');
DB::table('admin_panels')->insert(['discord' => $n, 'twitter' => 'anan']);
return response('helal');
}
我得到了这个错误:
Symfony\组件\HttpKernel\异常\ MethodNotAllowedHttpException 无消息
【问题讨论】:
-
我没有看到上面配置的任何
cross origin resource sharing。根据您对I want to post from my react native app to my laravel app的描述,这是一项要求。 -
我怎样才能正确添加它们,也许把它写成答案,如果它可以工作,我可以接受它作为答案
-
我按照你说的检查并添加到 Header 'Access-Control-Allow-Origin':'', 'Content-Type': 'multipart/form-data' ,现在它没有给出任何错误但一段时间后它给出了网络失败请求错误
标签: json laravel request fetch