Search code examples
javascriptjqueryiosfirefoxxmlhttprequest

iOS Firefox XMLHttpRequest wrong response


I have a function that send a XMLHttpRequest GET to retrieve JSON from my backend.

xhttp.onreadystatechange = function()
{
    if(xhttp.readyState == 4 && xhttp.status == 200)
    {
        applicationUtil.showLoader(false);
        if(handler != null)
        {
            alert(xhttp.responseText);
            if(xhttp.responseText != "" && this.getResponseHeader('content-type') == "application/json")
            {
                handler(JSON.parse(xhttp.responseText), false);
            }
            else
            {
                handler("", true);
            }
        }
    }
}

xhttp.open("GET", "../../csp/healthshare/hsanalytics/Custom.AMY.REST.Session.cls?CacheUserName="+ username +"&CachePassword="+ password, true);
xhttp.send(null);

Now, this function works 100% on any browser on any device, accept iOS Firefox. I have tried going the jQuery route, same results:

$.ajax({
      url: "../../csp/healthshare/hsanalytics/Custom.AMY.REST.Session.cls?CacheUserName="+ username +"&CachePassword="+ password,
      accepts:"application/json",
      cache: false,
      crossDomain: true,
      dataType: "json",
      username: username,
      password: password,
      error: function(event)
      {
          handler("", true);
      },
      success: function(data, status)
      {
          handler(data, false);
      } 
});

I have spent hours researching the topic, but I have not been able to find any articles specific to my problem.


Solution

  • Some browsers are very finicky when it comes to headers.. Have you tried to send basic authorization? Including username and password..

    e.g)

    {
    "Authorization": "Basic " + btoa(username + ":" + password)
    },
    

    Give it a go and see :)