Strange Behavior Parsing JSON Within IE8
I'm having strange behavior in IE8 that I can't explain executing the following JavaScript function:
function getStatus() {
var action = "/Device/Status";
$.getJSON(action, function (data) {
$.each(data, function (i, e) {
$('#btnEmergency' + e.BoreId).val(e.InEmergency ? "On" : "Off");
});
});
}
With Fiddler loaded, I verify the following JSON results are returned (notice InEmergency is false for all bores):
[{"BoreId":1,"InEmergency":false},{"BoreId":2,"InEmergency":false},{"BoreId":3,"InEmergency":false},{"BoreId":4,"InEmergency":false},{"BoreId":5,"InEmergen开发者_运维知识库cy":false},{"BoreId":6,"InEmergency":false}]
Strangely, e.InEmergency results to true for bore 1 (only in IE8 (I have not tested other versions of IE, I'm referring to the fact it works correctly in Chrome, Firefox and Safari)), resulting in the button value being set to "On". Am I missing something that should be obvious?
Internet Explorer has different caching behaviour compared to other browsers (inevitably). In particular, it tends to cache XMLHTTPRequest requests when that isn't useful.
The way around this is to add a useless parameter that changes with every request, to make it unique and therefore ensure that there is no cached value. Using the time in milliseconds is a very easy way to do this:
function getStatus() {
var action = "/Device/Status";
$.getJSON(action + '?_=' + (new Date()).getTime(), function (data) {
$.each(data, function (i, e) {
$('#btnEmergency' + e.BoreId).val(e.InEmergency ? "On" : "Off");
});
});
}
Note that (a) this is how jQuery does cache: false
in $.ajax
calls and (b) if you have already got parameters in your URL, you will need to use &_=
instead.
hi try using the browserss dom method eval() and see if that works out for you.. I am not sure if that is your problem. Else what I would suggest is to first evaluate the json strring to an object and then loop over the same abd verify it. My opinion is that looping is causing some problem in IE.
精彩评论