【问题标题】:Asp.Net / VB.Net webservice function call strange behaviourAsp.Net / VB.Net webservice 函数调用奇怪的行为
【发布时间】:2014-03-19 10:18:57
【问题描述】:

我正在使用 Visual Studio 2012 创建一个 webapp。

我正在尝试创建一个网络服务并从 javascript 调用一个函数。

这是我的网络服务:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    public Function HelloWorld(ToSomeone As string ) As String
        return "Hello World" + ToSomeone
    End Function

End Class

这是对网络服务的调用

<button class="customButton" onclick="return CallService()">ProvaWebService</button>
<div id="outputDiv" style="border: 1px solid black; height: 50px;"></div>
<script type="text/javascript">

function CallService() {
    Methodo_app.WebService1.HelloWorld("Yourself", onSuccess);
}

function onSuccess(result) {
    alert(result)
    var outDiv = document.getElementById("outputDiv");
    outDiv.textContent = result;
}
</script>

我认为它的所有配置都是正确的,因为我没有从 chrome/firefox 控制台收到错误,如果我调试脚本,我可以看到结果。但脚本结束后,结果会从页面中消失。

其实我有这种情况:

调试:

我在 div 中看到警报和文本,但在脚本结束后它们消失了

不调试:

我看不到结果(为什么警报不停止脚本??)

如果用这个更改脚本(我只添加一个警报)

<script type="text/javascript">

function CallService() {
    Methodo_app.WebService1.HelloWorld("Yourself", onSuccess);
    alert("hello")
}

function onSuccess(result) {
    alert(result)
    var outDiv = document.getElementById("outputDiv");
    outDiv.textContent = result;
}

</script>

我可以看到警告(结果)和 div 中的文本,但它们在脚本结束后仍然消失。

不知道是刷新问题还是同步问题

编辑:

我添加了一个onFail 函数,如果我运行对 web 服务的调用会失败,但如果我调试调用成功

【问题讨论】:

    标签: javascript jquery asp.net webservice-client


    【解决方案1】:

    错了

    return false;
    

    到函数

    function CallService() {
        Methodo_app.WebService1.HelloWorld("Yourself", onSuccess);                    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2021-12-06
      • 2010-09-30
      相关资源
      最近更新 更多