简体   繁体   English

jQuery ASP.net Web服务消耗

[英]jQuery ASP.net webservice consumption

I have been looking on how to consume a webmethod using the $.ajax call using this code below: 我一直在研究如何使用$ .ajax调用,并使用以下代码来消耗Web方法:

$.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. 但是,当我尝试将类型从“ POST”更改为“ GET”时,呼叫没有通过。 Can someone please point out a reason why this happens? 有人可以指出发生这种情况的原因吗?

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. 通过degault,ASP.Net AJAX Web服务的GET请求被禁用,ScottGu 在此方面拥有出色的博客条目 ,包括在您所需要的情况下如何绕过该安全性。

Here's an example fix, by setting UseHttpGet on the ScriptMethodAttribute : 这是一个示例修复,通过在UseHttpGet上设置ScriptMethodAttribute

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

As Nick wrote you can use ScriptMethodAttribute or you can enable GET request processing in web.config: 正如Nick所写,您可以使用ScriptMethodAttribute ,也可以在web.config中启用GET请求处理:

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

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. 另外,如果从Web服务检索数据,则逻辑上正确的方法通常是GET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM