wcf - jQuery 发布到 WCF 服务返回 405 错误

标签 wcf jquery

我最近在温习我的 jQuery,在查看帖子时遇到了一个问题,我无法从 wcf 服务获得响应,我不断收到 405 - 方法不允许。我的请求对我来说看起来不错,我想知道我是否错过了一些关键但明显的事情,说明为什么会发生这种情况。

这里是正在使用的邮政编码:

 $.ajax({
     type: "POST",
     url: "http://localhost:59929/CustomerService/GetCustomers",
     data: null,
     ContentType: "application/json",
     dataType: "json",
     success: function (msg) {
         alert("Called and got: " + msg);
     },
     error: function (result) {
         alert('Service call failed: ' + result.status + '' + result.statusText);
     }
 });

wcf代码如下:

[ServiceContract]
public interface ICustomerService
{   

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    List<Customer> GetCustomers();

    [OperationContract]
    OperationStatus InsertCustomer(Customer cust);
}

配置如下:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Fiddler 将原始帖子显示为:

POST http://localhost:59929/CustomerService/GetCustomers HTTP/1.1
Host: localhost:59929
Connection: keep-alive
Content-Length: 0
Origin: http://localhost:59513
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:59513/LearnJQuery2/ajax/ajax_post.htm
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

fiddler 也确认了 405 响应。

最佳答案

您从哪个域调用电话?这可能是由 Same origin policy 引起的。如果是这种情况,请尝试使用 JSONP 而不是 JSON。

另外,您是否尝试过像 Fiddler 这样的工具?准确查看请求/响应是什么?这可以让我们了解正在发生的事情。

关于wcf - jQuery 发布到 WCF 服务返回 405 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11829737/

相关文章:

javascript - 防止 jQuery 在生成 html 时执行分配给属性的函数

jquery - 显示文本一段时间

wcf - 我需要将哪些 WCF 客户端文件 checkin SVN

sql-server - Windows Azure 上的 WCF 服务 - 连接到本地 SQL Server 数据库

c# - WCF 操作行为 : ApplyDispatchBehavior not being called

jquery - 如何自定义 HTML/CSS 工具提示?

javascript - HTML 文件的国际化

c# - HTTP 请求未经客户端身份验证方案 'Negotiate' 授权。从服务器收到的身份验证 header 是 'NTLM'

asp.net - 如何在 REST Web 服务中处理使用 100 Continue?

用于在文本字段上自动完成选择的 jQuery 监听器