访问在 PageMethod 调用的回调函数中设置的 JavaScript 变量

发布于 2024-11-02 10:43:57 字数 1022 浏览 7 评论 0原文

在单击事件中,我调用 PageMethods 请求,其中包含两个回调函数:OnServerValidationSuccess 和 OnServerValidationFailure。我还尝试设置创建一个名为 clientResult 的对象;

    var clientResult = { rc: "", msg: "" };

    $('#btnSave').click( function () {
        PageMethods.ServerValidateCreateCompany(companyName, domainName, country, OnServerValidationSuccess, OnServerValidationFailure);
        alert(clientResult.rc); //this is null!
});

在OnServerValidationSuccess 中,result 参数包含异步响应。在那里我尝试将其分配给 clientResult

//On Success callback
function OnServerValidationSuccess(result, userContext, methodName) {
    clientResult = result;
    if (clientResult.msg.length != 0) {
        document.getElementById('resMsg').innerHTML = clientResult.msg;
        $('#resMsg').addClass('warning');
    }
}

我将 result 分配给回调函数中的本地 clientResult 变量。它应该包含完整形成的服务器响应,该响应由服务器端 WebMethod 正确发送。 但是,在调用 PageMethod 后,它仍然为 null 或为空。 为了将回调中设置的变量分配给局部变量以便我可以访问它,我必须做什么?

In a click event, I invoke a PageMethods request, which contains two callback functions: OnServerValidationSuccess and OnServerValidationFailure. I also try and set create an object called clientResult;

    var clientResult = { rc: "", msg: "" };

    $('#btnSave').click( function () {
        PageMethods.ServerValidateCreateCompany(companyName, domainName, country, OnServerValidationSuccess, OnServerValidationFailure);
        alert(clientResult.rc); //this is null!
});

In the OnServerValidationSuccess, the result parameter contains the asynchronous response. And in there I try to assign it to clientResult.

//On Success callback
function OnServerValidationSuccess(result, userContext, methodName) {
    clientResult = result;
    if (clientResult.msg.length != 0) {
        document.getElementById('resMsg').innerHTML = clientResult.msg;
        $('#resMsg').addClass('warning');
    }
}

I assign the result to local clientResult variable in the callback function. It should contain the fully formed server response, which is sent correctly by the server-side WebMethod.
However, it remains null or empty after PageMethod is called.
What must I do in order to assign the variable set in the callback to a local variable so that I can access it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-11-09 10:43:57

您似乎正确地将值分配给 clientResult ,您只是访问得太早了。

您正在进行异步/非阻塞调用,这意味着它会立即返回,但会继续在后台工作。因此,您的 JavaScript 会立即从对 PageMethods.ServerValidateCreateCompany 的调用中返回,并继续处理警报。但您的请求还没有时间完成,并且您的成功回调 (OnServerValidationSuccess) 甚至还没有运行,因此您会得到 undefinednull

您应该在 OnServerValidationSuccess 函数中调用将要处理响应的任何函数,该函数将在其中接收响应。也许是这样的(你似乎已经用 msg 做了一些事情,只需将你的工作与 rc 移到这里)

//On Success callback
function OnServerValidationSuccess(result, userContext, methodName) {
    clientResult = result;
    alert(clientResult.rc); //this will work
    process(clientResult); //process the response here
    if (clientResult.msg.length != 0) {
        document.getElementById('resMsg').innerHTML = clientResult.msg;
        $('#resMsg').addClass('warning');
    }
}

You seem to be assigning the value to clientResult correctly, you're just accessing it too early.

You're making an asynchronous/non-blocking call which means that it returns immediately but continues to work in the background. So your JavaScript immediately returns from the call to PageMethods.ServerValidateCreateCompany and moves on to the alert. But your request hasn't had time to complete and your success callback (OnServerValidationSuccess) hasn't even run yet so you get undefined or null.

You should invoke whatever function is going to process the response in the OnServerValidationSuccess function which is where the response will be received. Perhaps something like this (you already seem to doing something with msg, just move your work with rc here as well)

//On Success callback
function OnServerValidationSuccess(result, userContext, methodName) {
    clientResult = result;
    alert(clientResult.rc); //this will work
    process(clientResult); //process the response here
    if (clientResult.msg.length != 0) {
        document.getElementById('resMsg').innerHTML = clientResult.msg;
        $('#resMsg').addClass('warning');
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文