Asp.net 在ASMX Web服务中对同一WebMethod使用POST和GET Ajax调用

Asp.net 在ASMX Web服务中对同一WebMethod使用POST和GET Ajax调用,asp.net,ajax,service,asmx,Asp.net,Ajax,Service,Asmx,我似乎无法使用POST和GET从Ajax调用web服务方法 最初只有POST可以工作,GET会导致此错误: {“消息”:“试图 调用方法\u0027getData\u0027 使用GET请求,这不是 允许。”,“StackTrace”:“在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\r\n 在 System.Web.Script.Servi

我似乎无法使用POST和GET从Ajax调用web服务方法

最初只有POST可以工作,GET会导致此错误:

{“消息”:“试图 调用方法\u0027getData\u0027 使用GET请求,这不是 允许。”,“StackTrace”:“在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\r\n
在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData methodData)”,“ExceptionType”:“System.InvalidOperationException”}

我通过添加此属性修复了此问题:
[ScriptMethod(UseHttpGet=true)]
,但现在GET导致此错误:

{“消息”:“试图 调用方法\u0027getData\u0027 使用POST请求,这不是 允许。”,“StackTrace”:“在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\r\n
在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData methodData)”,“ExceptionType”:“System.InvalidOperationException”}

那么,您只能使用POST或GET,而不能从Ajax同时使用两者,这是真的吗?有人知道为什么会发生这种情况,或者是否有解决办法


提前谢谢

您可以配置一个ASMX服务来响应GET和POST,但我认为没有任何合理的方法让它们用JSON响应GET。如果没有JSON序列化,它们就不适合在AJAX调用中使用

如果希望通过GET请求JSON,则需要使用HttpHandler或WCF服务


另外,您应该。

您应该尝试使用WCF。ASMX web服务现在被认为是“遗留技术”,微软表示它们现在处于“维护模式”,不太可能修复bug。

ASMX web服务支持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
_
_
_
_
公共类测试服务
继承System.Web.Services.WebService
_
_
作为字符串的公共函数HelloWorld()
返回“你好,世界”
端函数
末级

为什么不拥有两个独立的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
_
公共函数HelloWorld_GET()作为字符串
返回“你好,世界”
端函数
_
公共函数HelloWorld_POST()作为字符串
返回“你好,世界”
端函数

我理解风险,但GET请求是用于JSONP的。很抱歉,太晚了,我刚刚意识到您希望GET和POST同时工作。如果我找到一个方法,我会让你知道。。。