2014-07-25 102 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

我更新了答案 –