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

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

问题描述

可能重复:
如何从AJAX返回响应从函数调用?

Possible Duplicate:
How to return the response from an AJAX call from a function?

我正在使用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;//doesnt goes here
            },
            error: function (textStatus, errorThrown) {
                Success = false;//doesnt goes 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 whats 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;//doesnt goes here
            },
            error: function (textStatus, errorThrown) {
                Success = false;//doesnt goes here
            }

        });
        //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称为

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

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

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