vini vini - 1 month ago 13
jQuery Question

Deferred jquery 201 Post created going in reject

postData: function (url, data) {
var deferred = Q.defer();

$.ajax({
type: 'POST',
url: url,
data: JSON.stringify(data),
dataType: "json",
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('Authorization', 'Bearer ' + window.access_token);
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
},
success: function (data, responseText, jqXHR) {
deferred.resolve(data, responseText, jqXHR);
},
error: function (e) {
deferred.reject(e);
}
});

return deferred.promise;
}


I don't understand why my request is going inside error callback

enter image description here

Answer

Based on comments that you are returning html you need to set appropriate dataType

$.ajax will also go to error if there is a parse error of the requested dataType. You can confirm this by logging the arguments of the error handler error: function (xhr, statusText, errorThrown)

Try changing

dataType:'json'

To

dataType:'html'

Or remove that property as jQuery will know from the Content Type header

Comments