从 http 处理程序返回的 JSON 在 IE8 中为空,但在 IE10 或 Chrome 中不是
JSON returned from http handler is null in IE8 but not IE10 or Chrome
>我有以下JavaScript
patients.prototype.GetPatient = function(patient_id,callback)
{
var xmlhttp;
var fullpath;
try {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var pat = parseJson(xmlhttp.response);
if (pat) {
callback(parseJson(xmlhttp.response));
}
else {
alert('Null object returned?');
}
}
else if (xmlhttp.status == 404) {
alert('Unable to find Retrieve Patient Service');
}
}
xmlhttp.open("GET", "RetrievePatient.ashx?PatientId=" + patient_id, true);
xmlhttp.send();
}
catch (e) {
alert('Unable to retrieve requested patient details');
}
}
function parseJson(jsonString) {
var res;
try {
alert('Parsing JSON');
res = JSON.parse(jsonString);
}
catch (e) {
alert('Call to evaluate result failed with error ' + e.message + ' Evaluating Json ' + jsonString );
};
return res;
}
如果这是从IE10上运行的页面运行的,我会正确返回患者详细信息。如果我在 Chrome 上运行它,它同样会返回患者的详细信息,但如果我在 IE8 中的页面上运行它,JSON 为空,整个事情就会失败。
有人知道我可以做些什么来在IE8中完成这项工作吗?
在尝试解析之前,请尝试检查 null。此外,添加对未定义的检查
function parseJson(jsonString) {
var res;
if (jsonString == undefined) {
return jsonString;
}
if (jsonString == null) {
return jsonString;
}
if (window.JSON && window.JSON.parse ) {
res = JSON.parse(jsonString);
return res;
}
}
相关文章:
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 如何将字符串拆分为字符,但在javascript中保留空格
- Console.debug在ie10中不工作
- JavaScript获胜't运行,但在jsFiddle中工作
- 列表样式在IE中未设置为none,但在IE中适用
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- 滚动在Chrome中有效,但在Firefox或IE中无效
- jQuery自动完成在Firefox中运行良好,但在Chrome中则不然
- Javascript的某些部分在Chrome中不起作用,但在其他浏览器中可以完美工作
- appendChild在函数外部工作,但在函数内部不工作
- 像createComment这样的各种自定义变量名在内联javascript中被覆盖,但在外部js中没有.为什么?
- video.js,播放视频,但在IE10上没有声音
- 从 http 处理程序返回的 JSON 在 IE8 中为空,但在 IE10 或 Chrome 中不是
- 在IE10(窗口中的“localStorage”)中,条件为真,但localStorage未定义
- AddSearchProvider 在 IE10 中已弃用,但在 IE11 中重新引入
- forEach在IE10上不能在Server 2008上工作,但在其他任何地方都可以在IE10中工作
- 在IE10中替换错误,但在IE8或Firefox中不替换
- jQuery AJAX跨域工作良好的Chrome等,但不发送数据在IE10和不发送请求在IE <= 9