【发布时间】: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