1

I have a PHP file. When I send category:'true' with the POST method in HTML JS, it gives me back text which I echo in PHP but with React Native it sends me an object instead. How can I solve this?

This is my code:

return fetch('http://709957ef.ngrok.io/follower/get%20json.php',{
        method:'POST',
        body:JSON.stringfy({
             category:'true'
        })
      })
        .then((response) => response.text())
        .then((responsetxt) => {
          alert(responsetxt)
        })
        .catch((error) => {
          console.error(error);
        });
    }
3
  • are you saying responsetxt is an object? Commented Jun 3, 2019 at 21:47
  • yes when i alert this android says: [object OBJECT] Commented Jun 3, 2019 at 23:05
  • then you just need to access a property on it that's the value you're looking for. Not sure why response.text() returns another promise. Commented Jun 4, 2019 at 1:37

1 Answer 1

1

change your code to and add async to our function.

componentDidMount = async () => {

  const response = await fetch('http://709957ef.ngrok.io/follower/get%20json.php',{
    method:'POST',
    body:JSON.stringify({
        category:'true'
    })
  })

  this.setState({
    data: response.text(),
  }, () => console.log('PHP DATA: ', this.state.data))
}
Sign up to request clarification or add additional context in comments.

12 Comments

i change my code to : ---- async componentDidMount() { const response = await fetch('709957ef.ngrok.io/follower/get%20json.php', { method: 'POST', body: JSON.stringfy({ category: 'true' }) }) return await (response.text()); } ---- but this code not work and react native warning is: "possible unhandled promisw rejection (id:0):typeError:undifined is not a function (evaluting'json.stringfy({ category:'true' })')"
don't return from there save it in state. please se my updated answer.
i changed but not back me anything and state was not changed and warning not solve
what do you get?
this is picture of warning : picture link
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.