javascript - AJAX success failure callback not working - Stack Overflow

I have a table and I have a delete button. Basically what I want to do is when I click on the delete bu

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. If it's successful, it will remove that row from the table.

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

        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

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. If it's successful, it will remove that row from the table.

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

        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

Share Improve this question asked Dec 7, 2016 at 19:07 singardsingard 1073 silver badges10 bronze badges 5
  • Are there any errors in the console log? If you check the browser's Network tab, do you see it sending the AJAX request? – Barmar Commented Dec 7, 2016 at 19:23
  • @Barmar there is no error in the console log. The ajax request is actually sent and I can see the row has been removed in the db – singard Commented Dec 7, 2016 at 19:28
  • Do you see the JSON response in network tab? – Barmar Commented Dec 7, 2016 at 19:34
  • @Barmar I didn't see the JSON response hmm – singard Commented Dec 7, 2016 at 19:37
  • Where in the documentation do you see a failure option? – Kevin B Commented Dec 7, 2016 at 19:43
Add a ment  | 

1 Answer 1

Reset to default 4

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). But the correct name of that option is error:, not 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744373584a4571074.html

相关推荐

  • javascript - AJAX success failure callback not working - Stack Overflow

    I have a table and I have a delete button. Basically what I want to do is when I click on the delete bu

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信