search

Home  >  Q&A  >  body text

javascript - react native uses fetch to transmit data to the backend and prompts an error

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_u1582phpcn_u15822755 days ago621

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:37:38

    Line 2 should be method, not methods

    reply
    0
  • 高洛峰

    高洛峰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...

    reply
    0
  • PHPz

    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.

    reply
    0
  • Cancelreply