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. docker绑定了nginx端口 外部访问不到2. python - 使用pandas的resample报错3. 网页爬虫 - python 爬取网站 并解析非json内容4. python - flask post提交timestamp不能作为参数,这是为什么?5. android - 类似于微信朋友圈图片这样的是在listview中嵌套gridview还是动态加入多个imageview呢?6. docker-machine添加一个已有的docker主机问题7. html - 类似这样的弹层用什么插件写比较好?8. vue.js - vue-router开启HTML5的history模式后nginx配置9. html5 - 图片一般一怎么的形式存放在服务器中的?10. node.js - mongodb查找子对象的名称为某个值的对象的方法

网公网安备