请问Ajax如何获取回调函数的返回值?
答案:2 悬赏:40 手机版
解决时间 2021-03-05 22:05
- 提问者网友:酱爆肉
- 2021-03-05 15:17
C# code
[WebMethod]
public static string GetPassword()
{
string password = CommonFunctions.GetMixPwd(6);
return password;
}
#endregion
javascript代码
function ResetPassword() {
return PageMethods.GetPassword(Password_CallBack);
}
function Password_CallBack(response) {
document.getElementById('hiddenPassword').value = response;
return confirm('The password has been reset to ' + response + ' , do you want to send email?');
}
HTML code
大侠帮帮忙了!或者指点一个替代的方法。谢谢!
最佳答案
- 五星知识达人网友:低音帝王
- 2021-03-05 16:15
function Password_CallBack(response) {
document.getElementById('hiddenPassword').value = response;
//这里取消 return confirm('The password has been reset to ' + response + ' , do you want to send email?');
//改为:
if(confirm('The password has been reset to ' + response + ' , do you want to send email?'))
{
//在这里再发起一次ajax请求,在server端实现发送email重置密码的通知.
//response就是你的hiddenPassword
//说实话,是不需要使用hiddenPassword这个控件的...response就已经是重置过的密码了.
}
}
全部回答
- 1楼网友:北方的南先生
- 2021-03-05 16:47
你好!
你的设计意图在引入ajax回调之后不可能实现了.
按你的设计目的,我建议你这样改进:
function password_callback(response) {
document.getelementbyid('hiddenpassword').value = response;
//这里取消 return confirm('the password has been reset to ' + response + ' , do you want to send email?');
//改为:
if(confirm('the password has been reset to ' + response + ' , do you want to send email?'))
{
//在这里再发起一次ajax请求,在server端实现发送email重置密码的通知.
}
}
另一种解决方案是:
public static string getpassword()加一个bool类型的参数sendemail,在server端直接实现发送email.
在调用ajax请求getpassword之前,先请用户 var sendemail=confirm('the password has been reset to ' + response + ' , do you want to send email?');
把confirm返回的结果作为参数传给getpassword方法.
最后 .一楼的解决方法看上去可行,但实际 运行时,a永远都是null.不会实现正确效果
有问题请再联系我,
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯