【问题标题】:How do I specify content-type with a AJAX call for Android browsers?如何使用 Android 浏览器的 AJAX 调用指定内容类型?
【发布时间】:2010-11-06 21:55:50
【问题描述】:

我有一个 ajax 调用发送到我的 rails 服务器。根据内容类型,我的 rails 服务器使用 HTML 或 JSON 响应。这在 iPhone 浏览器和桌面浏览器(如 chrome 等)上运行良好。但是在 Android 浏览器上,不知何故我的服务器无法识别内容类型。我认为 overrideMimeType 不起作用!

有人知道解决方法吗?如果我无法弄清楚,我只需要创建一个特殊的 URL 来处理 JSON 请求。

代码看起来像这样:

function makeAjaxCall {
    xmlhttp=new XMLHttpRequest();
    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();
}

【问题讨论】:

    标签: javascript android ajax json


    【解决方案1】:

    也许你需要

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

    还有吗?

    【讨论】:

      猜你喜欢
      • 2010-12-01
      • 2012-12-03
      • 2011-03-07
      • 1970-01-01
      • 2012-09-22
      • 1970-01-01
      • 2013-12-03
      • 1970-01-01
      相关资源
      最近更新 更多