【问题标题】:Jquery Ajax Call, doesn't call Success or Error [duplicate]Jquery Ajax调用,不调用成功或错误[重复]
【发布时间】:2013-01-23 09:05:23
【问题描述】:

可能重复:
How do I return the response from an asynchronous call?

我正在使用 Jquery Ajax 调用服务来更新值。

function ChangePurpose(Vid, PurId) {
    var Success = false;
    $.ajax({
        type: "POST",
        url: "CHService.asmx/SavePurpose",
        dataType: "text",
        data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            Success = true;//doesn't go here
        },
        error: function (textStatus, errorThrown) {
            Success = false;//doesn't go here
        }

    });
    //done after here
    return Success;
}

和服务:

[WebMethod]
public string SavePurpose(int Vid, int PurpId)
{
    try 
    {
        CHData.UpdatePurpose(Vid, PurpId);
        //List<IDName> abc = new List<IDName>();
        //abc.Add(new IDName { Name=1, value="Success" });
        return "Success";
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

正在从 AJAX 成功调用服务。 值也在发生变化。但是在服务之后,success:error:函数没有被调用,在这种情况下,应该调用成功,但它不起作用。

我用firebug发现,成功或者错误的函数都被跳过,直接转到return Success;

似乎找不到代码有什么问题。

更新: 添加async: false 解决了问题

【问题讨论】:

  • 这就是 Ajax 的工作原理……欢迎使用异步!如果它让你感到安慰:感觉至少有三个人每天问这个问题。
  • 您不知道如何正确检查代码。由于 AJAX 是异步的,因此断点可能更有帮助
  • 您在 Success = true 之前是否发出警报
  • 怀疑有人会回到这里,但这个“重复”更好的是它在标题中使用的词更有可能与遇到此问题的人的搜索相匹配。在其他答案的情况下,您必须特别了解该答案才能生成将您引导至该页面的搜索。当然,这个页面会引导你到那个页面,但仍然如此。
  • 尝试使用以下命令: ``` complete: function (xhr, status) { alert("Ajax 请求完成时调用"); } ```

标签: javascript web-services jquery


【解决方案1】:

将您的代码更改为:

function ChangePurpose(Vid, PurId) {
    var Success = false;
    $.ajax({
        type: "POST",
        url: "CHService.asmx/SavePurpose",
        dataType: "text",
        async: false,
        data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            Success = true;
        },
        error: function (textStatus, errorThrown) {
            Success = false;
        }
    });
    //done after here
    return Success;
} 

您只能从synchronous 函数返回值。否则你将不得不创建一个callback

所以我刚刚在您的 ajax 调用中添加了 async:false,

更新:

jquery ajax 调用默认是异步的。因此,当 ajax 加载完成时,将调用成功和错误函数。但是您的 return 语句将在 ajax 调用启动后立即执行。

更好的方法是:

     // callbackfn is the pointer to any function that needs to be called
     function ChangePurpose(Vid, PurId, callbackfn) {
        var Success = false;
        $.ajax({
            type: "POST",
            url: "CHService.asmx/SavePurpose",
            dataType: "text",
            data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                callbackfn(data)
            },
            error: function (textStatus, errorThrown) {
                callbackfn("Error getting the data")
            }
        });
     } 

     function Callback(data)
     {
        alert(data);
     }

并将 ajax 称为:

 // Callback is the callback-function that needs to be called when asynchronous call is complete
 ChangePurpose(Vid, PurId, Callback);

【讨论】:

  • 那个……那个。工作!非常感谢。但是你怎么解释呢?
  • @Ruchan:在我的第一条评论中阅读链接到的问题。它解释了它以及为什么使用这个解决方案 (async:false) 不好。
  • 我理解(Felix Kling),但是有什么解决方法呢?我需要在那里显示消息。
  • @Felix Kling:你是对的。更新了我的答案。 Ruchan:如果你需要同步调用,你可以使用 async:false(在这种情况下,如果没有 ajax 脚本等,你的代码将无法继续)
  • 有人对我投了反对票?
【解决方案2】:

尝试将 ajax 调用封装到一个函数中,并将 async 选项设置为 false。请注意,此选项自 jQuery 1.8 起已弃用。

function foo() {
    var myajax = $.ajax({
        type: "POST",
        url: "CHService.asmx/SavePurpose",
        dataType: "text",
        data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
        contentType: "application/json; charset=utf-8",
        async: false, //add this
    });
    return myajax.responseText;
}

你也可以这样做:

$.ajax({
    type: "POST",
    url: "CHService.asmx/SavePurpose",
    dataType: "text",
    data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
    contentType: "application/json; charset=utf-8",
    async: false, //add this
}).done(function ( data ) {
        Success = true;
}).fail(function ( data ) {
       Success = false;
});

您可以阅读更多关于jqXHR jQuery Object

【讨论】:

  • 是的,添加 async: false working 。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-07-31
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
相关资源
最近更新 更多