0

I have the following code setup on a webpage(private at this time):

function DoRequest(httpReq,url,param){

  httpReq.open("POST",url,false);
  httpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  httpReq.send(param);
  httpReq.onreadystatechange = function() {
   if (httpReq.readyState==4) {alert(httpReq.responseText);}
  }

  if(httpReq.status==200){
    return httpReq.responseText;
  } else {
    return httpReq.status;
 }
}

When I run a request through this, via firefox, safari, ie 7-9, chrome etc on the pc I get a 200 status and the text back correctly.

When I run this same exact page on my iphone, or ipad, I get a status of 0, response text of nothing, and an alert from that callbackfunction of the correct response text afterword. Which seems like it is not waiting for the request to finish before proceeding through the code for the mobile safari browsers, is there anyway to better force it to wait, or a flag I'm blind to see?

I also only get the onreadystate callback on safari, it doesn't do it in any other browser, I tried removing the function in case that was somehow triggering it to run async instead of of not but no dice there it still runs async despite the 3rd parameter being false.

I also just tried httpRq.open("POST",url); without the 3rd parameter and without the onreadystatechange function and still runs async.

The url is an absolute path.

  • It's likely a network issue since you're testing it from two different network and since URL isn't provided for testing. – Jay Sep 28 '12 at 00:34
  • It's from the same network, on the same wi-fi, I can't provide a link cause it's an NDA project. The url is absolute so http://domain/pathtophpfile, on everything else it's sync it waits until the call is done and continues on with the code, this one continues on with the code with the responses of 0 and blank, the request still goes through because it makes adjustments in the database from doing the call, it just doesn't wait for the response that sais the adjustment was made correctly. – user1703884 Sep 28 '12 at 14:29
  • I think I found my answer in this post: http://stackoverflow.com/questions/10076614/alternatives-for-javascript-synchronous-xmlhttprequest-as-timing-out-in-safari?rq=1 – user1703884 Sep 28 '12 at 15:19

1 Answers1

2

Mobile Safari does not support synchronous use of XMLHttpRequest (third parameter to open is false in your code, which is unsupported).

Anything that depends on the results of the request should get moved into a callback function, and called in your onreadystatechange function right where you have the alert() call.

Matt DiMeo
  • 1,095
  • 8
  • 11