So, I have a basic HTML page using jQuery 1.6.2 and I have an MVC application that serves data on a separate domain (The HTML page is local, the MVC application is not). The MVC application is sending back a JSON string just fine; the problem is that when sending back dates, there are a “/” before and after the date which breaks the JSON string, which in turn does not get turned into a javascript object. Phew.
Here is my AJAX call:
$.ajax({
url:url,
dataType: 'jsonp',
success: function(data, status, jqXHR) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR); console.log(textStatus); console.log(errorThrown);
}
});
Using the “network” tab in the Chrome profiler, I can see that a JSON string is being returned, and it has the correct data, but has some malformed syntax. Here is a sample of that:
"DateAuthorized":"\/Date(1307789505000-0500)\/",
I tried to use dataType “text”, but because this is a cross-domain request, I have to use jsonp.
Any ideas what-so-ever? Can the MVC application use a regex to remove the slashes? Is there a way to use javascript XHttpRequest to build a manual request?
Agh!
It’s using the default serializer which is the JavaScriptSerializer which has always had this issue. An alternative is to use JSON.NET to serialize your objects.
You can always implement a replacement from the client-side — parseJSON extension
…Or format it from the server-side like this before sending it back: