开发者

How do I specify content-type with a AJAX call for Android browsers?

开发者 https://www.devze.com 2023-01-24 00:53 出处:网络
I have an ajax call that goes out to my rails server. Depending on the content type my rails server either responds with HTML or JSON. This works fine on the iPhone browser and on desktop browsers lik

I have an ajax call that goes out to my rails server. Depending on the content type my rails server either responds with HTML or JSON. This works fine on the iPhone browser and on desktop browsers like chrome, etc. However on the Android browser somehow my server is not recognizing the content type. I don't think the overrideMimeType is working!

Anyone know of a workaround? If I can't figure it out I'll just have to create a special URL to handle the JSON requests.

Code looks something like this :

function makeAjaxCall {
    xmlhttp=new XMLHtt开发者_Python百科pRequest();
    targetUrl = window.location.pathname;
    xmlhttp.open('GET',targetUrl,true);     
    xmlhttp.overrideMimeType("application/json");
    xmlhttp.onreadystatechange=function()
    {

      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
              // On android browsers only this responds with HTML when it should be JSON
        alert('response:' + xmlhttp.responseText);
        r = eval('(' + xmlhttp.responseText + ')');
                    // Do more stuff
      }
    }
    xmlhttp.send();
}


Maybe you need

xmlhttp.setHeader("Content-Type", "application/json");

as well?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号