我在Chrome扩展程序中有一些代码可以成功使用then()
的xhr响应中的$.ajax
。
if (request && request.action === "updateTask") {
$.ajax({
type: "PUT",
url: config.updateTaskApiUrl(storage.apiKey(), request.calendarEntryId),
data: request.data,
dataType: "json"
}).then(function(data) {
sendResponse(data);
});
}
然而,我现在删除了一条记录并做了同样的事情:
if (request && request.action === "deleteTask") {
$.ajax({
type: "DELETE",
url: config.updateTaskApiUrl(storage.apiKey(), request.calendarEntryId),
dataType: "json"
}).then(function(data) {
debugger
sendResponse(data);
});
}
上面的debugger
行永远不会被击中。我已尝试done()
并将success: function() {}
作为密钥也在通话中。
API的响应返回200。
知道为什么它永远不会到达then()
?
答案 0 :(得分:0)
看起来因为返回状态代码,它被解释为失败。我抓住它并检查了故障功能中的状态:
$.ajax({
type: "DELETE",
url: config.updateTaskApiUrl(storage.apiKey(), request.calendarEntryId),
dataType: "json"
}).then(function(data) {
sendResponse(data);
}, function(data){
if (data.status == 200) {
sendResponse(data);
}
});