react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[XMLHttpRequest] GET request is not working in react-native android but working in react-native ios

Open prashanthmerugucs opened this issue 11 years ago • 7 comments

var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
  if (request.readyState !== 4) {
    return;
  }

  if (request.status === 200) {
    console.log('success', request.responseText);
  } else {
    console.warn('error');
  }
};

request.open('GET', 'https://mywebsite.com/endpoint.php');
request.send();

my url is like : https://mywebsite.com/gen?method=getMethod&data={"userId" : "User2cc75dc3-44b0-45c3-4532-"}

prashanthmerugucs avatar Sep 29 '15 11:09 prashanthmerugucs

Can you provide more details on the error you're getting?

astreet avatar Sep 29 '15 12:09 astreet

With Get request am getting request.status == 0 and the error message is like java.net.URI: https://cloudapp.com/gen?method=getMethod&data={%22userId%22 : %22user2chhjej%22}

but with POST request am getting request.status == 200

prashanthmerugucs avatar Sep 30 '15 04:09 prashanthmerugucs

What is the request.responseText when request.status == 0?

astreet avatar Sep 30 '15 10:09 astreet

@prashanthnani - can you provide a real endpoint and exact code that we can test this with?

brentvatne avatar Sep 30 '15 15:09 brentvatne

I am having the same problem here and trying to reach any get request from android (including things like www.google.com)

I happen to be using clojurescript and the function call for me is simply

(GET "http://www.google.com"
{:error-handler #(print "caused error")
:handler #(print "worked")})

There is nothing else that is needed. PS changing the same function above to simply POST works immediately. This was checked on a Nexus 5 as well as on AVD simulator.

mackram avatar Apr 06 '16 12:04 mackram

@prashanthnani, @mackram did you check the ATS settings in Xcode project?

iOS requires HTTPS calls, unless you create exceptions as documented: https://facebook.github.io/react-native/docs/integration-with-existing-apps.html#app-transport-security

Alternatively (but not recommended) add the following to your Info.plist:

  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
  </dict>

This is however not recommended, and your app might be rejected from AppStore submission.

josephmiclaus avatar Sep 08 '16 14:09 josephmiclaus

I'm seeing this for POSTs also. Could this be related to timeout somehow? Occurs when I introduce it.

asbjornenge avatar Jan 23 '17 20:01 asbjornenge