tags:

views:

196

answers:

2

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');
    },

    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 :(

+1  A: 

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.

Sky Sanders
im going to accept this as the answer since i found out that the JSON parser of jquery is not that dependable and using json2 helped solved my problem
lock
+1  A: 

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.

Dave Ward
+1 for corroboration ;-)
Sky Sanders
i always turn on those tools whenever i work on JS so its not a prob...i'll be testing out the json2
lock
@lock, If you are going to be working with JSON you should always include json2.js. It will only install itself if there is not a native implementation present, and I would venture a guess that more than 50% of browsers in use do not have a native implementation.
Sky Sanders
so i included json2 and modified the data submission in case that's the real cause, but the problem still persists, everything works fine for other browsers except IE,is there a way that would let jquery tell me what error actually went wrong while it's trying to retrieve the json object?
lock
@lock - well.... a good first step would be to examine the error returned, which, by your code, you are not. `error: function(xhr){alert(xhr.responseText);}` would be a start. ( sorry dave, the convo just moved to your comments.)
Sky Sanders
Here's a walkthrough on catching and displaying the JSON serialized error information that comes back from ScriptServices and page methods, using jQuery: http://encosia.com/2009/03/04/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/
Dave Ward