javascript - react native uses fetch to transmit data to the backend and prompts an error
phpcn_u1582
phpcn_u1582 2017-05-16 13:35:38
0
3
648

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.

phpcn_u1582
phpcn_u1582

reply all(3)
我想大声告诉你

Line 2 should be method, not methods

小葫芦

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...

PHPzhong

should be used postData='lalala', which is related to jquery search rules. You can learn more about it in detail.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template