Backbone - Getting raw response from server

2019-09-07 05:07发布

问题:

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...