2014-07-25 67 views
0

我正在編寫我的第一個基於HTML5的應用程序,我也在編寫我的第一個Web服務並嘗試兩者之間的內部連接。也便於試驗檯爲了這個,我已經建立了簡單的本地.vb基於Web的服務,這是如下:掙扎從jQuery調用返回數據到vb.Net asmx web服務

<System.Web.Script.Services.ScriptService()> _ 
<WebService(Namespace:="http://tempuri.org/")> _ 
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Public Class Verify 
Inherits System.Web.Services.WebService 

<WebMethod()> _ 
Public Function HelloWorld() As String 
    Return "Hello World" 
End Function 

<System.Web.Services.WebMethod()> _ 
Public Function UsernameVerify(ByVal username As String) _ 
    As Boolean 
    Return (username = "Username") 
End Function 

<System.Web.Services.WebMethod()> _ 
Public Function PasswordVerify(ByVal pass As String) _ 
    As Boolean 
    Return (pass = "RandomPassword123") 
End Function 
End Class 

我想測試我的應用程序能夠連接到Web服務,發送數據,並收到適當的回報。要做到這一點我用jQuery的:

function verifyInfo(){ 
     $.ajax( 
     { 
      Type: 'Post', 
      url: 'http://localhost/TestWebService/Verify.asmx/HelloWorld', 
      success: function(data){alert(data);}, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       debugger; 
      } 
     });    
    } 

現在的問題是,在存儲數據的值將輸出爲[object Document],我不能完全肯定從這裏到繼續。從調用HelloWorld函數調用,我希望返回一個字符串Hello World。當我在Visual Studio中通過調試做一個基於瀏覽器的測試所調用做工精細,並返回相應的XML輸出,如:

<string xmlns="http://tempuri.org/">Hello World</string>

對不起,這麼長的文章,但希望能得到關於如何獲得一些指導期望的數據在POST服務之後返回作爲回報。

非常感謝您提供任何提示/提示/建議。

+0

我有一種感覺的一部分issu e是我的webservices POST'Content-Type:application/x-www-form-urlencoded'這可能是導致我的問題的一部分,因爲它不是json對象? – JDD

回答

2

ASMX以「d」包裝器的形式返回結果。看到這個很不錯的文章http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/

所以你可以試試:

success: function(data){alert(data.d);} 

UPDATE:也試圖定義你的Ajax調用適當的內容類型:

$.ajax({ 
    type: 'POST', 
    url: [your url] 
    data: [your data], 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    success: function (data) {alert(data.d);} 
}); 

更新2:嘗試也改變你的web方法到這個(添加一個響應格式):

<WebMethod()> _ 
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ 
Public Function HelloWorld() As String 
    Return "Hello World" 
End Function 
+0

非常感謝您將現在閱讀。 – JDD

+0

我遵循這些指示ivan.sivak但問題仍在繼續。看到我對OP的評論。 – JDD

+0

我更新了答案 –