2010-06-22 67 views
0

我已成功從JSON格式的ASP.Net webservice(使用不需要參數的服務方法)返回數據,但一直在努力進行需要參數的web服務調用。使用JSON將數據傳遞到ASP.NET webservice

的Webservice:

<WebMethod()> _ 
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _ 
Public Function TestWebService(ByVal Description As String) As Stock 
    Dim res As New Stock(Guid.NewGuid, Description) 
    Return res 
End Function 

對象:

Public Class Stock 

    Public Sub New() 
    End Sub 

    Public Sub New(ByVal StockID As Guid, ByVal Description As String) 
     Me._StockID = StockID 
     Me._Description = Description 
    End Sub 


    Public Property StockID As Guid 
    Public Property Description As String 

End Class 

的Javascript:

client = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); 
client.onreadystatechange = DataReturnedFromHttpRequest; 
client.open("GET", "/MyWebService.asmx/TestWebService?" + JSON.stringify({"Description":["Test"]}), true); 
client.setRequestHeader("Content-Type", "application/json"); 
client.send(null); 

響應:

{"Message":"Invalid web service call, missing value for parameter: \u0027Description\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} 

我明白錯誤,但似乎無法正確解決如何格式化我的請求。

回答

1

終於明白了......所以對於任何好奇的人來說,答案都是瑣碎的。

GET請求必須採用以下格式

/MyWebService.asmx/MyWebserviceMethod?Param1=%22ParamValue1%22&Param2=%22ParamValue2 

然後,它就像一個魅力。