How can I pass POST parameters in fetch request? - React Native

前端 未结 2 1251
长情又很酷
长情又很酷 2021-02-07 19:29

This is my code:

componentWillMount() {
  fetch(\"http://10.4.5.114/localservice/webservice/rest/server.php\", {
    method: \'POST\',
    body: JSON.stringify({         


        
2条回答
  •  再見小時候
    2021-02-07 19:51

    This what worked for me

    fetch("http://10.4.5.114/localservice/webservice/rest/server.php", {
      method: 'POST',
      headers: new Headers({
                 'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
        }),
      body: "param1=value1¶m2=value2" // <-- Post parameters
    })
    .then((response) => response.text())
    .then((responseText) => {
      alert(responseText);
    })
    .catch((error) => {
        console.error(error);
    });
    

提交回复
热议问题