【问题标题】:Jquery ajax call always returns error on ASP.NET web serviceJquery ajax 调用总是在 ASP.NET Web 服务上返回错误
【发布时间】:2013-10-10 10:14:22
【问题描述】:

我一直在尝试从 php 应用程序对 ASP.NET Web 服务进行 jquery ajax 调用。我试过 jsonp 但结果总是一样的。它总是给我一个错误结果,当我试图查看错误时,它只会给我一个空白结果。我已经尝试添加和删除 ajax 调用的属性以查看它是否有效,但仍然没有结果。至于网络服务,我 100% 确定它工作正常。

这是我的 ajax 调用的代码:

function submitClicked(){
        var url = "http://localhost/MyWebService/service1.asmx/HelloWorld";
        $.ajax( url, {
                dataType: "jsonp", 
                type:'POST',  
                success: function (data) {  
                    successCallback(data);  
                },
                error:function(error){
                    console.log("error");
                }

        });
}

这是我在 VB.NET 中的 Web 服务代码:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Sub HelloWorld()
    Context.Response.Clear()
    Context.Response.ContentType = "application/json"
    Context.Response.Flush()
    Context.Response.Write("{""success"":1}")
End Sub

任何帮助将不胜感激。谢谢。

干杯。

【问题讨论】:

    标签: asp.net web-services jquery


    【解决方案1】:

    好吧,我知道我的错误是什么。

    为了帮助遇到同样问题的任何人,我在我的 Web 服务方法中添加了一个参数,即回调参数,如下所示:

     <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Sub HelloWorld(ByVal Test As String, ByVal callback As String)
        Dim json As String = "{""success"":1}"
        Dim sb As StringBuilder = New StringBuilder()
        sb.Append(callback + "(")
        sb.Append(JsonConvert.SerializeObject(json))
        sb.Append(");")
        Context.Response.Clear()
        Context.Response.ContentType = "application/json"
        Context.Response.Write(sb.ToString)
        Context.Response.End()
    End Sub
    

    至于ajax函数,它是这样的:

    $.ajax({
                    url: "http://localhost/MyWebService/Service1.asmx/HelloWorld",
                    crossDomain:true,
                    type: 'POST',
                    dataType: "jsonp",
                    cache: false,
                    data:{Test:'This is a test'},
                    success:function(data){
                        var json = $.parseJSON(data);
                        if(json.success == 1) {
                             alert("success");
                        }
                        else
                        {
                             alert("failed");
                        }
                    },
                    error:function(error){
                        alert(error);
                    }
                });     
    

    希望对您有所帮助。谢谢你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 2016-01-07
      • 2015-11-18
      • 1970-01-01
      • 2013-10-01
      相关资源
      最近更新 更多