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

发布于 2024-08-18 03:14:39 字数 1030 浏览 1 评论 0原文

我似乎无法同时使用 POST 和 GET 从 Ajax 调用 Web 服务方法。

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

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

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

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

那么,您是否只能使用 POST 或 GET 而不能同时使用 Ajax 中的两者之一?有谁知道为什么会发生这种情况或者是否有解决方法?

提前致谢!

I can't seem to call a web service method from Ajax with both POST and GET.

Initially only the POST would work and GET would causes this error:

{"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"}

I fixed that by adding this attribute: [ScriptMethod(UseHttpGet=true)] but now GET causes this error:

{"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"}

So is it true that you can only use either POST or GET and not both from Ajax? Does anyone know why this occurs or if there is a workaround?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

总以为 2024-08-25 03:14:39

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

如果您想通过 GET 请求 JSON,则需要使用 HttpHandler 或 WCF 服务。

另外,您应该在暴露之前确保您知道自己在做什么通过 GET 获取 JSON

You can configure an ASMX service to respond to both GET and POST, but I don't believe there's any reasonable way to make them respond to GETs with JSON. Without the JSON serialization, they aren't really appropriate for use in AJAX calls.

If you want to request JSON via GET, you'll need to use an HttpHandler or WCF service.

Also, you should be sure that you know what you're doing before exposing JSON via GET.

薄凉少年不暖心 2024-08-25 03:14:39

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

ASMX webservices support JSON GET's using the following syntax.

<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
清眉祭 2024-08-25 03:14:39

为什么不只拥有两个独立的 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

Why not just have two seperate web services, one for GET and one 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
墨落成白 2024-08-25 03:14:39

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

You should try this with WCF. ASMX web services are now considered to be "legacy technology", and Microsoft has said they are now in "maintenance mode", and are unlikely to have bugs fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文