user1703884
user1703884

Reputation: 1

forcing non-async XMLHttpRequest in mobile safari

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.

Upvotes: 0

Views: 1898

Answers (1)

Matt DiMeo
Matt DiMeo

Reputation: 1136

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.

Upvotes: 2

Related Questions