jQuery Ajax调用,不调用成功或错误 [英] Jquery Ajax Call, doesn't call Success or Error

查看:79
本文介绍了jQuery Ajax调用,不调用成功或错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何返回来自异步调用?

Possible Duplicate:
How do I return the response from an asynchronous call?

我正在使用Jquery Ajax调用服务以更新值.

I am using Jquery Ajax to call a service to update a value.

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成功调用该服务. 值也正在更改.但是,在服务出现后,成功:错误:功能被未调用,在这种情况下,应该调用成功但不能成功.

the service is being called Successfully from the AJAX. Value is also being Changed. But after the Service, success: or error: functions are not being called, in this case success should have been called but it is not working.

我使用了firebug,发现成功或错误功能被跳过,直接进入return Success;

I used firebug and found that, the success or error functions are being skipped and goes directly to return Success;

似乎找不到代码的问题.

Can't seem to find what's the problem with the code.

更新: 添加async: false可以解决问题

Update: adding async: false fixed the problem

推荐答案

将您的代码更改为:

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.

You can only return the values from a synchronous function. Otherwise you will have to make a callback.

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

So I just added async:false, to your ajax call

更新:

jquery ajax调用在默认情况下是异步的.所以成功与ajax加载完成后,将调用错误函数.但是您的return语句将在ajax调用开始后立即执行.

jquery ajax calls are asynchronous by default. So success & error functions will be called when the ajax load is complete. But your return statement will be executed just after the ajax call is started.

更好的方法是:

     // 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:

and call the ajax as:

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

这篇关于jQuery Ajax调用,不调用成功或错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆