I’m making a request to an old server that serves up ill-formed JSON. It’s not mine, so I can’t modify the server. It’s cross-domain. Here’s my code:
$.ajax({
url: 'http://someDomain.com/getData.htm',
dataType: 'jsonp',
error: function(jqXHR, textStatus, errorThrown) {
alert("Got an error: " + textStatus + " " + errorThrown);
},
success: function(data) {
alert("Got it: " + data);
}
});
As it stands, of course, I get:
Got an error: parseerror Jquery12379789584794587_2893798579279874978 was not called.
So it’s choking because the JSON-esque response looks like this:
{name:"RMA-83186",date:"01/24/12 13:30:45"}
…and jQuery rightfully requires it to be well-formed JSON. (It’s not because the properties should be double-quoted. See the “Important” note here.)
So, I thought I’d bring the response in as dataType: 'text', clean it up and then let jQuery parse it as JSON. However, when I change it to dataType: 'text' I get:
Got an error: error No transport
…which is what you get from a cross-domain request. I tried adding crossDomain: true to the request and using it with the text dataType, but got the same error.
Anyone know how I can get this ill-formed data in from a cross-domain request so I can clean and parse it?
Thanks!
EDIT: The dataFilter parm doesn’t help because the root problem (when using dataType:'jsonp') is not even the ill-formed JSON string, but rather that the response is not a true JSONp response, i.e., it’s not a function call.
The only suggestion I think would work would be to use an intermediary server side proxy that you control. This way you can format the data correctly and feed it back to your JavaScript. You could also use a Yahoo! Pipe.
The problem is that a JSONP call is really just a new script element added to the DOM. And you can’t request plain text because of the same origin policy. Also, since the server doesn’t return proper JSON, I’m sure it won’t work with a CORS request. Therefore, you will have to use the proxy.