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. boot2docker无法启动2. docker-compose中volumes的问题3. java - SSH框架中写分页时service层中不能注入分页类4. docker容器呢SSH为什么连不通呢?5. dockerfile - 为什么docker容器启动不了?6. docker安装后出现Cannot connect to the Docker daemon.7. nignx - docker内nginx 80端口被占用8. node.js - antdesign怎么集合react-redux对input控件进行初始化赋值9. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””10. 关于docker下的nginx压力测试

网公网安备