2010-01-20 33 views
3

我似乎無法使用POST和GET從Ajax調用Web服務方法。在ASMX Web服務中使用POST和GET Ajax調用相同的WebMethod

最初只有POST將工作和GET會導致此錯誤:

{"Message":"An attempt was made to call the method \u0027getData\u0027 using a GET request, which is not allowed.","StackTrace":" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

我固定,通過增加該屬性:[ScriptMethod(UseHttpGet=true)]但現在得到的原因這個錯誤:

{"Message":"An attempt was made to call the method \u0027getData\u0027 using a POST request, which is not allowed.","StackTrace":" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

因此,它是確實,您只能使用POST或GET,而不能同時使用Ajax?有誰知道爲什麼會發生這種情況,或者是否有解決方法?

在此先感謝!

回答

2

可以配置一個ASMX服務來響應GET和POST,但我不認爲有任何合理的方法可以使他們響應GET與JSON。沒有JSON序列化,它們並不適合在AJAX調用中使用。

如果您想要通過GET請求JSON,則需要使用HttpHandler或WCF服務。

另外,你應該be sure that you know what you're doing before exposing JSON via GET

+0

我理解的風險,但GET請求都是爲了與JSONP使用。 – celticpride 2010-01-20 14:41:14

0

你應該試試WCF。 ASMX網絡服務現在被認爲是「傳統技術」,微軟已經表示他們現在處於「維護模式」,並且不太可能修正錯誤。

1

ASMX webservices支持JSON GET使用以下語法。

<System.Web.Script.Services.ScriptService()> _ 
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ 
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ 
<ToolboxItem(False)> _ 
Public Class TestService 
    Inherits System.Web.Services.WebService 

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


End Class 
+0

抱歉,遲到了,只是意識到你希望獲得和帖子同時工作。如果我找到方法,會讓你知道... – 2011-01-01 18:09:31

1

爲什麼不只是有兩個單獨的Web服務,一個是GET和POST一個

<WebMethod()> _ 
<ScriptMethod(UseHttpGet:=True)> 
Public Function HelloWorld_GET() As String 
    Return "Hello World" 
End Function 

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