7

固定的!谢谢!请参阅下面的“更正代码”。

目标是从对话框中取回数据。我看过很多文章,但无法让其中任何一篇工作,所以我决定使用 Web 服务在对话框和底层页面之间来回传递数据。

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

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

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

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');

固定代码:

function getLocal(name) {
var rtn = "";
    $.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) {
            rtn = data.d;
        }
    });
return rtn;
}
4

3 回答 3

9

它违背了 AJAX 同步使用它的目的(AJAX 代表 Asynchronous Javascript And Xml)。

现在你不能return从成功方法中获取值,但你可以将它存储在一个变量中,然后返回它

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;
}

正确的方法是使用延迟对象

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

并称之为

"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");
    });
}
于 2012-12-12T20:49:37.183 回答
5

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

正确的方法是这样做:

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);
        }
    });
}

然后,您的主要代码必须看起来像这样(异步代码):

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

要改进此代码,一次进行两次异步调用,您可以使用jQuery Deferred对象,并.resolve(data)在所有 ajax 调用获得正确响应后运行它。

于 2012-12-12T20:57:20.643 回答
0

我通过 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

于 2020-11-08T10:19:23.430 回答