2

我正在尝试使用 Jquery ajax 调用来调用 WCF REST 服务方法并收到类似的错误

"NetworkError: 405 Method Not Allowed - http://localhost:55911/Service1.svc/Testing"

这是我的代码

  $(document).ready(function () {
            $("#Button2").click(function () {
                 var Input = {
                     UserId: "11111"
                             };
                $.ajax({
                    type: "POST",
                    url: " http://localhost:55911/Service1.svc/Testing",
                    data: JSON.stringify(Input),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("Success");
                    },
                    error: function (xhr, status, error) {
                        alert("failure");
                        alert(stattus.toString);
                    }
                });

            });
        });

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Testing", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Testing(int UserId);

  public string Testing(int UserId)
        {
            sqlConn = new SqlConnection(connectionString);
            sqlConn.Open();
            sqlCmd = new SqlCommand("TestInsertion", sqlConn);
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.Parameters.Add("@id",UserId);
            sqlCmd.ExecuteNonQuery();
            sqlConn.Close();
            return "true";
        }

我在这里做错了什么?有什么建议吗?

编辑:评论后 //contentType: "application/json"发布和投掷

"NetworkError: 400 Bad Request - http://localhost:55911/Service1.svc/Testing"
4

6 回答 6

1

您定义的UriTemplate错误,请尝试像这样使用它:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Testing?U={UserId}", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Testing(string UserId);

然后在实现中根据UserId需要将其转换为 int 。

于 2013-02-14T06:19:19.587 回答
1
  • 请检查 HTTP 请求方法是否POST在客户端 ( html) 和服务器 ( WCF service) 上
  • 因为 NetworkError 405 状态表明调用代码 ( html) 使用了错误的 HTTP 方法,WCF service所以工作正常。

请参考:WCF 中的 405 Method Not Allowed Error

更新

我相信保留其余的东西(也是内容类型contentType: "application/json"),因为它只是将参数类型更改为stringfrom intinWCF service

    public string Testing(string UserId)
    {
        sqlConn = new SqlConnection(connectionString);
        sqlConn.Open();
        sqlCmd = new SqlCommand("TestInsertion", sqlConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.Add("@id",UserId);
        sqlCmd.ExecuteNonQuery();
        sqlConn.Close();
        return "true";
    }
于 2013-02-12T11:22:24.497 回答
0

您是否同时运行 WCF 服务项目和消费项目(AJAX 调用)(在 Visual Studio 的同一实例中)?如果是这样,请尝试两个不同的 VS 实例并先运行 WCF 项目,然后再运行消费项目。另外,尝试使用dataTypeasjsonp而不是json.

于 2013-02-12T11:29:07.073 回答
0

我认为您需要使用 JSONP 进行跨域调用以绕过浏览器限制

这个答案很有帮助:WCF 错误:405 Method Not Allowed

于 2014-03-04T16:30:21.623 回答
0

检查 HTTP 请求方法是 POST 还是 OPTIONS。此链接将很有用。

于 2013-02-18T12:26:53.973 回答
0

通常,当您的方法引发异常时会发生此错误,因此我建议调试

它可以通过在开头编写这行代码将其附加到处理

功能

Debugger.launch();
于 2013-02-12T11:09:03.207 回答