【问题标题】:WebService method returns "undefined" back to javascript, even though it should return a valueWebService 方法将“未定义”返回给 javascript,即使它应该返回一个值
【发布时间】:2016-08-19 21:58:24
【问题描述】:

我有一个返回booleanWebService's 方法:

[WebMethod(EnableSession = true), ScriptMethod(UseHttpGet = true)]
public bool StartMonitoring()
{
    return Schedule.StartMonitoring();
}

这是 Schedule 类的方法:

public static bool StartMonitoring()
{
    return true;
}

这是JavaScript's服务电话:

var success = false;
success = myself.get_Service().StartMonitoring();
alert(success);

当弹出窗口显示时,它显示"undefined" 而不是true

我错过了什么?

【问题讨论】:

  • myself 指的是什么?
  • 如果你像这样直接从StartMonitoring 返回true 会发生什么:public bool StartMonitoring(){ return true; } - 只是看看你是否真的得到了任何价值。
  • 我假设 get__Service 或 StartMonitoring 是您所说的服务。由于服务是异步调用,因此您不能期望成功。尝试使用异步方法,如回调。

标签: javascript asp.net web-services


【解决方案1】:

不确定您使用的是哪种 AJAX 代理技术,但我很确定您需要回调,因此您的 JS 代码应该看起来更像这样:

myself.get_Service().StartMonitoring(myCallback);

function myCallback(result)
{
     alert(result);
}

【讨论】:

    猜你喜欢
    • 2015-03-03
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2012-07-07
    相关资源
    最近更新 更多