Asp.Net / VB.Net webservice函数调用的奇怪行为

Asp.Net / VB.Net webservice function call strange behaviour

本文关键字:Net 函数调用 VB webservice Asp      更新时间:2023-09-26

我正在使用Visual Studio 2012创建一个web应用程序。

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

这是我的webservice:

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

这是对web服务

的调用
<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函数,我看到如果我运行调用webservice将失败,但如果我调试调用它的成功

return false;

到函数

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