访问在 PageMethod 调用的回调函数中设置的 JavaScript 变量
在单击事件中,我调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正确地将值分配给
clientResult
,您只是访问得太早了。您正在进行异步/非阻塞调用,这意味着它会立即返回,但会继续在后台工作。因此,您的 JavaScript 会立即从对
PageMethods.ServerValidateCreateCompany
的调用中返回,并继续处理警报
。但您的请求还没有时间完成,并且您的成功回调 (OnServerValidationSuccess
) 甚至还没有运行,因此您会得到undefined
或null
。您应该在
OnServerValidationSuccess
函数中调用将要处理响应的任何函数,该函数将在其中接收响应。也许是这样的(你似乎已经用msg
做了一些事情,只需将你的工作与rc
移到这里)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 thealert
. But your request hasn't had time to complete and your success callback (OnServerValidationSuccess
) hasn't even run yet so you getundefined
ornull
.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 withmsg
, just move your work withrc
here as well)