问题
Since upgrading from 1.1.3 to 1.4.2 I've been getting uncaught JS errors when Breeze tries to parse responseText that is in HTML format, not JSON. In version 1.1.3, the code is wrapped in a try/catch, which catches the parsing error. The error is thrown on "JSON.parse(XHR.responseText)". But since it's caught, this works great. The error message is bubbled up to the caller.
var err = new Error();
err.XHR = XHR;
err.message = XHR.statusText;
err.responseText = XHR.responseText;
err.status = XHR.status;
err.statusText = XHR.statusText;
if (err.responseText) {
try {
var responseObj = JSON.parse(XHR.responseText);
err.detail = responseObj;
if (responseObj.ExceptionMessage) {
err.message = responseObj.ExceptionMessage;
} else if (responseObj.InnerException) {
err.message = responseObj.InnerException.Message;
} else if (responseObj.Message) {
err.message = responseObj.Message;
} else {
err.message = XHR.responseText;
}
} catch (e) {
}
}
return err;
In 1.4.2 there is no try/catch and i get an uncaught error. I don't know if I'm supposed to catch this myself. I didn't have to do anything in the previous version.
function extractErrors(XHR) {
if (!XHR.responseText) return null;
var responseObj = JSON.parse(XHR.responseText);
return responseObj && responseObj.EntityErrors;
}
Thanks
回答1:
I would upgrade to latest version of Breeze which is now 1.4.6. This version does have a try/catch around the new HttpResponse object that is a wrapper over the XHR.
来源:https://stackoverflow.com/questions/20294436/breeze-js-parsing-xhr-responsetext