c# - 如何允许 C# Web 服务中的方法从网站检索数据?

标签 c# web-services rest

这是我的 restful web 服务代码。我试图从网站获取一些数据。但是发生错误。它说“NetworkError: 405 Method Not Allowed”我可以做些什么来防止这种情况发生。

p>
    namespace WebService1
    {
       [System.Web.Script.Services.ScriptService]
       [WebService(Namespace = "http://microsoft.com/webservices/")]
       [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
       [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
       public class Service1 : System.Web.Services.WebService 
       {
            public Service1()
            {
            }

            [WebMethod]
            [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public string sayHelloJson(string name)
            {
                string strgreeting = string.Format("Hello {0}", name);
                JavaScriptSerializer js = new JavaScriptSerializer();
                return js.Serialize(strgreeting);
            }
        }
    }

这是我的 html 代码。我使用 wamp 服务器运行它。

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>

    <script type="text/javascript">
    function Greeting(){

         var Name=$('#txtname').val();

            $.ajax({
            type: "POST",
            url: 'http://localhost:54884/Service1.asmx?op=ssayHelloJson',
            //data: ({ name : 'ggg' }),
            dataType: "html",
             contentType: "application/x-www-form-urlencoded",
            success: function(data) {
                // Run the code here that needs
                //    to access the data returned
                alert( data);
            },
            error: function() {
                alert('Error occured');
            }
        });
    }

    </script>


</head>
<body>

<form id="form1" >
    <div>
        <p>json format call</p>
        Enter name:<input type="text" id="txtname" />
        <input type="button" id="btngo" value="GO" onclick="Greeting()" />


    </div>
    <p id="result"></p>


</form>

</body>
</html> 

最佳答案

 <script type="text/javascript">
    function Greeting(){

         var Name=$('#txtname').val();

            $.ajax({
            type: "POST",
//your method name in webservice change like this 
            url: 'http://localhost:54884/Service1.asmx/ssayHelloJson',
           data: ({ name : 'ggg' }),
            dataType: "POST",
             contentType: "application/x-www-form-urlencoded",
            success: function(data) {
                // Run the code here that needs
                //    to access the data returned
                alert( data);
            },
            error: function() {
                alert('Error occured');
            }
        });
    }

    </script>

和你的网络配置添加服务获取和发布请求

<location path="Service1.asmx">
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</location>

关于c# - 如何允许 C# Web 服务中的方法从网站检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18072287/

相关文章:

php - 无法连接到 WSDL

java - 不使用 main() 发布 JAX-WS Web 服务的最佳方式?

http - 休息 API : HTTP Status code for auth violation

rest - 不存在 ID 为 *id* 的消息

c# - 添加性能计数器类别挂起计算机

iphone - 如何在不同的iPhone上检测同一用户?

c# - 从 VSTS 将 nuget 包推送到 nuget.org 时忽略重复项

javascript - 循环时await 调用会并行执行吗?

c# - 包装托管代码以供非托管使用

c# - 关于在 C# 中将运算符作为 ENUM 中的值的问题