My react native code is as follows:
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);
});
My backend PHP code is like this:
<?php
$res = $_POST['postData'];
echo $res;
?>
最后在手机APP上弹出这个错误:TypeError:Body not allowed for GET or HEAD requests
May I ask what is the reason? I see that some data that needs to be transmitted on the Internet is of formData type. Please enlighten me.
高洛峰2017-05-16 13:37:38
The direct reason is due to the spelling error of ‘method’. The reason behind this is that due to a spelling error, the default request method is a ‘GET’ request. The HTTP/1.1 protocol specification does not support carrying data in the body for ‘GET’ requests, and parameters can only be passed through the URL. For details, please refer to http://stackoverflow.com/ques...
PHPz2017-05-16 13:37:38
should be used postData='lalala'
, which is related to jquery search rules. You can learn more about it in detail.