javascript - react native 使用fetch 向后端传数据提示错误
问题描述
我的react native 代码如下:
fetch(’https://www.lisonblog.cn/apptest/test.php’,{ methos: ’POST’, headers: {’Content-Type’: ’application/json’, }, body: JSON.stringify({postData:’lalala’ })}).then(function(res) { alert(res.status) if (res.status === 200) {return res.json() } else {return Promise.reject(res.json()) }}).then(function(data) { alert(data)}).catch(function(err) { alert(err);});
我的后端的PHP代码是这样的:
<?php $res = $_POST[’postData’]; echo $res;?>最后在手机APP上弹出这个错误:TypeError:Body not allowed for GET or HEAD requests
请问是什么原因?我看网上有的需要传输的数据是formData类型的,求大神赐教
问题解答
回答1:第2行,应该是method,不是methos
回答2:直接原因是由于 ‘method’ 拼写错误。背后原因是由于拼写错误导致默认请求方式为 ‘GET’ 请求, 二 HTTP/1.1协议规范里 对于 ‘GET’ 请求不支持在 body 里携带数据,参数只能通过URL传递。具体可参考 http://stackoverflow.com/ques...
回答3:应该使用postData=’lalala’,跟jquery查找规则相关,具体可再了解。
相关文章:
1. ueditor上传服务器提示后端配置项没有正常加载,求助!!!!!2. python - 关于matplotlib的x轴显示的问题3. 绝对定位和fied定位,键盘弹起对布局的影响4. angular.js - angularjs 怎么封装 upload 上传5. python - Pycharm一句代码写完可以自动补全空格么?6. javascript - vue生成一维码?求助!!!!!急7. nginx英文文档的WebSocket proxying部分没看太明白,麻烦推荐一点中文文章8. python 计算两个时间相差的分钟数,超过一天时计算不对9. android 文件File删除问题10. angular.js - 怎样在使用ng-repeat属性的标签里面监听单个事件,使其能够单个改变CSS。
