bizhan shabani
bizhan shabani

Reputation: 13

Fetch API work in postman but do not work in react native

I want to fetch my API in react native but react does not show my result, this API showing in postman but do not show in react native, after I change my API this works for me but does not show in this API. My code is :

fetch('http://roomarket.ir/LlIi1/CT.php')
  .then((response13) => response13.json())
    .then((response3) => {
      this.setState({Alert.alert(response3.ENtime)})

How do I fetch it in react native? Please help me.

Upvotes: 0

Views: 1869

Answers (2)

Saurabh Nemade
Saurabh Nemade

Reputation: 1582

This looks like a CORS Headers issue. Example fiddle which shows fetch is failing with different domain name : https://jsfiddle.net/98to6za0/

fetch('http://roomarket.ir/LlIi1/CT.php')
  .then((response13) => response13.json())
    .then((response3) => {
    		console.log(response3);
    });

With postman it will work since there are no cors controls enforced on the url.

API need to be enabled for cors : CORS with php headers

You can read more about CORS here : https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Upvotes: 0

Samitha Nanayakkara
Samitha Nanayakkara

Reputation: 2497

Try this.

fetch('http://roomarket.ir/LlIi1/CT.php')
  .then((response) => response.json())
    .then((responseData) => {
      this.setState({response:responseData. ENtime},() => Alert.alert(this.state.response))

Upvotes: 1

Related Questions