[英]AJAX success failure callback not working
I have a table and I have a delete button. 我有一张桌子,我有一个删除按钮。 Basically what I want to do is when I click on the delete button, it will send a DELETE request to our server and update on the db.
基本上,我要做的就是单击删除按钮,它将向我们的服务器发送一个DELETE请求并在数据库上更新。 If it's successful, it will remove that row from the table.
如果成功,它将从表中删除该行。
Config.js Config.js
var SiteConfigProxy = function () {
"use strict";
var getSiteConfig = Config.apiUrl + "/configs/site/{siteId}";
var addDevice = Config.apiUrl + "/configs/device/{deviceId}";
var delete1 = function (url, data, done, fail){
$.ajax({
type: "DELETE",
url: url,
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
if (typeof (done) === 'function') done(data);
},
failure: function (data) {
if (typeof (fail) === 'function') fail(data);
}
});
};
return {
deleteDeviceConfig: function (deviceId, data, done, fail){
delete1(addDevice.replace("{deviceId}", deviceId), data, done, fail);
}
};
} ();
siteconfig.js siteconfig.js
configTable.on("click", ".delete", function (event) {
event.preventDefault();
if (confirm("Are you sure to delete this row?")) {
var tableRow = $(this).parents("tr")[0];
var data = {
siteId: tableData.fnGetData(tableRow)[1],
hostname: tableData.fnGetData(tableRow)[2]
};
SiteConfigProxy.deleteDeviceConfig(tableData.fnGetData(tableRow)[0], data,
function(data){
tableData.fnDeleteRow(tableRow);
console.log("delete succeeded");
},
function(data){
console.log("delete failed");
}
);
}
});
Right now every time when I click on the button, it won't remove that row and the console won't log that statement. 现在,每次我单击该按钮时,它都不会删除该行,并且控制台也不会记录该语句。 I am just wondering why the success and failure callback isn't working.
我只是想知道为什么成功和失败回调不起作用。
Thanks a lot 非常感谢
Since your server script is not returning a valid JSON response, it should invoke the failure callback (because attempting to parse the response as JSON fails). 由于您的服务器脚本未返回有效的JSON响应,因此它应调用失败回调(因为JSON失败时尝试解析响应)。 But the correct name of that option is
error:
, not failure:
. 但是该选项的正确名称是
error:
而不是failure:
So change it to: 因此将其更改为:
error: function (data) {
if (typeof (fail) === 'function') fail(data);
}
If the server isn't supposed to send back a JSON response, leave out the dataType:
option, and it won't try to parse it. 如果服务器不应该发送回JSON响应,请省略
dataType:
选项,它将不会尝试解析它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.