How do I get the raw response returned by the server when using POST/DELETE methods in Backbone (so you can't get the raw response in fetch
/parse
like GET)?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can access the responseText
from the server when the ajax call is complete.
The save
, fetch
, etc methods from Backbone provide the jQuery promise generated by the call to $.ajax
.
When this promise has been resolved (e.g. the transaction is complete), you'll have access to the responseText
property on the promise.
var promise = a_model.save();
$.when(promise).then(function(){
console.log(promise.responseText); // if you're getting XML back it's `responseXML`
});
Here's the jQuery docs on the jqXHR object which is the promise object you get.
Additionally $.when
and .then
are described in the Deferred object documentation
回答2:
Why don't you just use the network tab in Chrome developer tools? That's the easiest way of viewing your raw requests and responses. Plus it is laid out in an easy to view format, showing cookies, timing values etc...