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"}
我明白錯誤,但似乎無法正確解決如何格式化我的請求。