简体   繁体   English

呼叫终止前已成功调用AJAX

[英]AJAX Success called before call terminated

I am writing a method to delete a town via jQuery and Ajax. 我正在编写一种通过jQuery和Ajax删除城镇的方法。 Here is my code: 这是我的代码:

<button class="btn btn-danger btn-sm" onClick="deleteTown(@item.TownId)"><i class="fa fa-trash"></i></button></a>

Delete Town method: 删除城镇方法:

    function deleteTown(townId) {
    console.log(townId);
    $.ajax({
        type: "POST",
        url: "/Tenant/DeleteTownCritera",
        dataType: "json",
        async : false, 
        data: { Id: townId, TenantId: @Request.QueryString["tenantId"] },
        success: processDeleteTownResult()
    });
}

Success method: 成功方法:

    function processDeleteTownResult(data) {
    console.log(data);

    if(data.Success == 1)
        {
        $("#town" + townId).remove();
    }
}

Controller: 控制器:

    [HttpPost]
    public JsonResult DeleteTownCritera(CriteriaInput input)
    {
        TenantLogic logic = new TenantLogic();
        var x = logic.DeleteTownCritera(input);

        return Json(x, JsonRequestBehavior.AllowGet);
    }

What's happening? 发生了什么?

I have a breakpoint set on my controller. 我在控制器上设置了一个断点。

Essentially, I hit my button which called deleteTown. 本质上,我点击了名为deleteTown的按钮。

Console logs town id. 控制台记录镇ID。

Console logs undefined. 控制台日志未定义。

Console errors: Uncaught TypeError: Cannot read property 'Success' of undefined. 控制台错误:未捕获的TypeError:无法读取未定义的属性“成功”。

The controller is then hit with the correct TownId and TenantId. 然后用正确的TownId和TenantId击中控制器。

Any suggestions of why the success method is being terminated before the ajax call is? 关于为什么成功方法在ajax调用之前终止的任何建议?

Thanks, Dan 谢谢,丹

First get the response and then you need to call your function by passing this response as parameter:- 首先获取响应,然后您需要通过将此响应作为参数传递来调用函数:

success: function(response){
  processDeleteTownResult(response);
}

It's being called because you've included parenthesis here: 之所以被称为是因为您在此处包括了括号:

success: processDeleteTownResult()

Replace with this: 替换为:

success: processDeleteTownResult

Or 要么

success: function(response){processDeleteTownResult(response);}

(as suggested in comments) (如评论中所建议)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM