开发者

valid json still fails on IE with jquery's ajax or getJSON callbacks

开发者 https://www.devze.com 2022-12-23 22:16 出处:网络
everytime my page loads, im supposed to create a datatable (also a jquery plugin) but when im fetching the contents, using .ajax or .getJSON always goes straight ahead to the error function, without e

everytime my page loads, im supposed to create a datatable (also a jquery plugin) but when im fetching the contents, using .ajax or .getJSON always goes straight ahead to the error function, without even telling me what went wrong inside the callback

$.ajax({
    cache: false,
    type: "POST",
    url: oSettings.sAjaxSource,
    data: {'newdate' : date},
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(json) {
        console.log('retrieving json data');
    },开发者_JS百科

    error: function() {
        console.log("An error has occurred. Please try again.");
    }
});

that's the actual code with the callback stripped for security purposes...

this works fine in firefox which actually executes what's on the callback function but IE simply fails and proceeds to writing my log

i've read alot that the primary reason the JSON calls fails for IE is whenever there are trailing commas or simply malformed JS

but i used JSONLint already and verified that my json object is a valid one :(


Try this

 data: '{"newdate" : "' + date + '"}',

The better choice is to use json2.js to ensure that there is a valid JSON implementation resident and then serialize your data before calling ajax

var postData = JSON.stringify(data);
...
 data: postData,
...

Do not rely on jquery to do this for you.


Since you're setting the contentType to application/json, I'm assuming you're calling an ASMX ScriptService or Page Method. In that case, what Sky Sanders suggests is correct. You must supply the data parameter as a JSON string; otherwise jQuery will serialize the object incorrectly as key=value&key=value pairs.

However, another thing to keep in mind is that IE throws an error when JavaScript code tries to utilize the console object, unless the development tools (and I believe the console itself) have been opened before that code runs. That is often the cause of IE-specific issues when console.log is involved.

0

精彩评论

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

关注公众号