jQuery 1.52 and dataFilter conversions
Hi all,
It looks like jQuery 1.52 has broken some custom JSON parsing for me. I use custom JSON decoding to allow for date parsing. I've been using this code for a long time back to jQuery 1.3 and it's just now with version 1.52 that this is breaking.
The $.ajax() code I'm using looks something like this where I'm using a custom dataType (serviceproxy) to bypass the default decoding pipeliine (but I've also tried this with text with the same result):
- $.ajax({
url: url,
data: jsonData,
type: "POST",
contentType: _I.contentType,
timeout: _I.timeout,
dataType: "serviceproxy", // custom type to avoid double parse
dataFilter: function (jsonString, type) {
if (type == "serviceproxy") {
// Use json library so we can fix up dates
var res = JSON.parseWithDate(jsonString);
if (res && res.hasOwnProperty("d"))
res = res.d;
return res;
}
return jsonString;
},
success: function (result) {
if (callback)
callback(result);
},
error: function (xhr, status) {
var err = null;
if (xhr.readyState == 4) {
var res = xhr.responseText;
if (res && res.charAt(0) == '{' && status != "parsererror")
var err = JSON.parse(res);
if (!err) {
if (xhr.status && xhr.status != 200)
err = new CallbackException(xhr.status + " " + xhr.statusText);
else {
if (status == "parsererror")
status = "Unable to parse JSON response.";
else if (status == "timeout")
status = "Request timed out.";
else if (status == "error")
status = "Unknown error";
err = new CallbackException("Callback Error: " + status);
}
err.detail = res;
}
}
if (!err)
err = new CallbackException("Callback Error: " + status);
if (errorHandler)
errorHandler(err, _I, xhr);
}
});
The dataFilter function gets called and my code correctly deserializes the JSON content. However, jQuery in 1.52 will continue to process the returned result as JSON and I get a Unable to parse JSON error.
In 1.51 jQuery would not further process the result from the dataFilter and I would get the correct response returned.
The custom data type I use was an attempt to get jQuery to ignore the content and not parse it but it looks like in 1.52 jQuery is trying to be too smart for itself and ignore my dataType setting and sniff the value directly out of the respone content type. Haven't drilled into the code if this is so but based on the error that gets returned jQuery is clearly doing some JSON processing that fails.
FWIW, the whole dataFilter pipeline is pretty weak in jQuery. Even in 1.51 and earlier if I returned a string result (which is a valid decoded result) jQuery would want to continue processing this result. As a result we had to use some nasty code that checks the result type and then restringifies.
- $.ajaxSetup({ dataFilter: function (jsonString, type) {
- if (type == "json") {
- // Use json library so we can fix up dates
- var res = JSON.parseWithDate(jsonString);
- if (typeof (res) === "string")
- return jsonstring; // return JSON as jQuery double parses
- if (res && res.hasOwnProperty("d"))
- return (typeof res.d === "string") ?
- JSON.stringify(res.d) : // jQuery double parses
- res.d;
- return res;
- }
- return jsonString;
- }
- });
This API implementation needs some way for the develoeper to specify that processing of this data is complete and that jQuery should not perform any more conversions on it.
It certainly it looks like a bug to me, and I don't see anything in the 1.52 changelog that pertains to this, but there's definitely a breaking behavior change in 1.52 for me.
Any idea on how I can work around this?
+++ Rick ---