0

i want to fetch my api in react native but react do 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 do not show 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

  • Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – sideshowbarker Nov 11 '18 at 08:15

2 Answers2

1

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))
Samitha Nanayakkara
  • 2,529
  • 2
  • 10
  • 23
0

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

Saurabh Nemade
  • 1,522
  • 2
  • 11
  • 20