jQuery ASP.net Web 服务消耗

发布于 2024-09-13 21:07:51 字数 423 浏览 1 评论 0原文

我一直在研究如何使用下面的代码使用 $.ajax 调用来使用 webmethod:

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebService.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            }
        });

但是,当我尝试将类型从“POST”更改为“GET”时,调用没有通过。有人可以指出发生这种情况的原因吗?

I have been looking on how to consume a webmethod using the $.ajax call using this code below:

$.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebService.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            }
        });

However when I tried to change the type from "POST" to "GET", the call didn't go through. Can someone please point out a reason why this happens?

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

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

发布评论

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

评论(3

短叹 2024-09-20 21:07:51

默认情况下,ASP.Net AJAX Web 服务禁用 GET 请求,ScottGu 在这方面有一篇出色的博客文章,包括如何绕过该安全性(如果是这样的话)你在追求什么。

下面是一个修复示例,通过设置 < ScriptMethodAttribute< 上的代码>UseHttpGet /代码>:

[WebMethod, ScriptMethod(UseHttpGet=true)] 
public string HelloWorld() 
{
  return "Hello World";
}

By degault GET requests are disabled for ASP.Net AJAX Web services, ScottGu has an excellent blog entry on this, including how to bypass that security if that's what you're after.

Here's an example fix, by setting UseHttpGet on the ScriptMethodAttribute:

[WebMethod, ScriptMethod(UseHttpGet=true)] 
public string HelloWorld() 
{
  return "Hello World";
}
空城仅有旧梦在 2024-09-20 21:07:51

正如 Nick 所写,您可以使用 ScriptMethodAttribute 或在 web.config 中启用 GET 请求处理:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>

As Nick wrote you can use ScriptMethodAttribute or you can enable GET request processing in web.config:

<webServices>
    <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
    </protocols>
</webServices>
生生不灭 2024-09-20 21:07:51

您是否尝试过检查服务器端代码以查看它从哪里提取值?根据所使用的方法,它可能不会响应。

顺便说一句:如果您从 Web 服务检索数据,逻辑上正确的方法通常是 GET。

have you tried checking the server side code to see where it pulls values from? it may not respond depending on the method used.

also an aside: if you are retrieving data from a web service, the logically correct method is usually GET.

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