jQuery返回值未定义

jQuery返回值未定义,jquery,Jquery,修正!谢谢请参阅下面的“更正代码” 目标是从对话框中获取数据。我看过很多文章,但没有一篇成功,所以我决定使用web服务在对话框和底层页面之间来回传递数据 除了读取从web服务返回的值的代码外,所有代码都已就位。我可以在调试器中看到数据被传回,但是当我返回到调用者时,返回的数据是未定义的 jQuery函数getLocal调用AJAX,返回良好的数据,但是当它返回到调用它的函数(verbListShow)时,返回的值是“未定义的” 这一切都发生在ASP.NET页面中,该页面主要使用jQuery编写,

修正!谢谢请参阅下面的“更正代码”

目标是从对话框中获取数据。我看过很多文章,但没有一篇成功,所以我决定使用web服务在对话框和底层页面之间来回传递数据

除了读取从web服务返回的值的代码外,所有代码都已就位。我可以在调试器中看到数据被传回,但是当我返回到调用者时,返回的数据是未定义的

jQuery函数getLocal调用AJAX,返回良好的数据,但是当它返回到调用它的函数(verbListShow)时,返回的值是“未定义的”

这一切都发生在ASP.NET页面中,该页面主要使用jQuery编写,并打开一个jQuery对话框

function getLocal(name) {
    $.ajax({
        type: "POST",
        async: false,
        url: "WebServices/FLSAService.asmx/GetLocalVariable",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ name: name }),
        success: function (data) {
            var rtn = data.d;
            return rtn;
        }
    });
}
上面的代码可以工作,但是调用时,rtn是未定义的。这是来电者:

function verbListShow(dutyNumber) {

    $('#dlgDutyList').dialog({
        modal: true,
        show: "slide",
        width: 250,
        height: 250,
        open: function (event, ui) {
            setLocal("DUTYNUMBER", dutyNumber);
        },
        buttons: {
            "Select": function () {
                var id = getLocal("VERBID"); // <*** Returns undefined
                var verb = getLocal("VERB"); // <*** Returns undefined
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "WebServices/FLSAService.asmx/SetDuty",
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    data: JSON.stringify({ dutyNum: dutyNumber, id: id, verb: verb }),
                    success: function (data) {
                        data = $.parseJSON(data.d);
                        if (data.ErrorFound) {
                            showMessage(data.ErrorMessage, 2, true);
                        }
                        else {
                            log('Set Duty: ' + data.StringReturn + ' (' + data.intReturn + ')');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("updateDuty: "
                            + XMLHttpRequest.responseText);
                    }
                });

                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }

    });
    $('#dlgDutyList').dialog('open');

同步使用AJAX违背了AJAX的目的(AJAX代表异步Javascript和Xml)

现在,您不能
从success方法返回值,但可以将其存储在变量中,然后返回该值

function getLocal(name) {
    var returnValue;
    $.ajax({
        type: "POST",
        async: false,
        url: "WebServices/FLSAService.asmx/GetLocalVariable",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ name: name }),
        success: function (data) {
            returnValue = data.d;
        }
    });
    return returnValue;
}

但正确的方法是使用

叫它

"Select": function() {
    var results = {};
    var self = this;
    $.when(getLocal("VERBID", results), getLocal("VERB", results)).then(function(){
        $.ajax({
            type: "POST",
            url: "WebServices/FLSAService.asmx/SetDuty",
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({
                dutyNum: dutyNumber,
                id: results.VERBID,
                verb: results.VERB
            }),
            success: function(data) {
                data = $.parseJSON(data.d);
                if (data.ErrorFound) {
                    showMessage(data.ErrorMessage, 2, true);
                }
                else {
                    log('Set Duty: ' + data.StringReturn + ' (' + data.intReturn + ')');
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("updateDuty: " + XMLHttpRequest.responseText);
            }
        });
    }).always(function(){
        $(self).dialog("close");
    });
}

一切都是因为$.ajax函数不返回任何值,因为它是异步行为,我的建议是为
getLocal
方法设置第二个参数,称为“callback”

正确的方法是这样做:

function getLocal(name, callback) {
    $.ajax({
        type: "POST",
        async: false,
        url: "WebServices/FLSAService.asmx/GetLocalVariable",
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({ name: name }),
        success: function (data) {
            var rtn = data.d;
            callback(rtn);
        }
    });
}
然后,您的主代码必须如下所示(异步代码):


为了改进这段代码,要同时进行两次异步调用,您可以使用object,在所有ajax调用都获得正确响应之后,在其上运行
.resolve(data)

我通过set
async:false
解决了这个问题

我创建了一个新的全局函数,名为
sendRequest(type,url,data)
,每次在任何地方调用三个参数

function sendRequest(type, url, data) {
    let returnValue = null;
    $.ajax({
        url: url,
        type: type,
        async: false,
        data: data,
        dataType: 'json',
        success: function (resp) {
            returnValue = resp;
        }
    });
    return returnValue;
} 
现在调用函数

        let data = {
            email: 'tet@gmail.com',
            password: 'warning',
        };
        let  response =  sendRequest('POST', 'http://localhost/signin')}}", data);
        console.log(response );
重要提示注意代码中的是:
async:false


web服务在向其发布名称时返回什么?里面有d字段吗?显示一些示例数据..
返回rtn无法从ajax成功回调返回。我建议不要使用getLocal函数,或者让它返回jqXHR对象。多亏了这两个响应者。由于你的帮助,我的代码运行得很快。
//some code here
buttons: {
            "Select": function () {
                getLocal("VERBID", function(id) {
                     getLocal("VERB", function(verb) { 
                        $.ajax({
                           type: "POST",
                           async: false,
                           url: "WebServices/FLSAService.asmx/SetDuty",
                           dataType: 'json',
                        //some code here
                     });
                });
//some code here
function sendRequest(type, url, data) {
    let returnValue = null;
    $.ajax({
        url: url,
        type: type,
        async: false,
        data: data,
        dataType: 'json',
        success: function (resp) {
            returnValue = resp;
        }
    });
    return returnValue;
} 
        let data = {
            email: 'tet@gmail.com',
            password: 'warning',
        };
        let  response =  sendRequest('POST', 'http://localhost/signin')}}", data);
        console.log(response );