2013-10-10 148 views
0

我一直在試圖從php應用程序中調用一個jQuery Ajax調用ASP.NET web服務。我試過用jsonp,但結果總是一樣的。它總是給我一個錯誤的結果,當我試圖看到錯誤時,它只給了我一個空白的結果。我試着添加和刪除ajax調用的屬性來查看它是否工作,但仍然沒有結果。至於網絡服務,我100%肯定它工作正常。Jquery ajax調用總是返回ASP.NET web服務上的錯誤

因此,這裏是我的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 

任何幫助將高度讚賞。謝謝。

乾杯。

回答

0

嗯,我想出了什麼是我的錯誤。

爲了幫助那裏的人誰具有同樣的問題,我增加了一個參數,以我的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); 
       } 
      });  

希望它幫助。謝謝。

相關問題