1

我想从客户端使用 AJAX 将 JSON 对象传递给 WCF 服务。在 Internet Explorer 中一切正常,但在 Firefox 中却不行。

在 Firefox 中我得到一个405:Method not allowed

这是我将 json 数据(来自客户端脚本)传递给 WCF 服务的地方......

    $(document).ready(function () {
        var Author = '{ "Id": "A01", "Name": "Ravinder" }';
        $.ajax({
            type: "POST",
            data: JSON.stringify(Author),
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: "http://localhost:53905/Service1.svc/AuthorPostByJson",
            success: function (data) {
                alert("success");
            },

            error: function (xmlhttprequest, textstatus, errorthrown) {
                alert(" failed ");
                console.log("error: " + errorthrown);
            }
         });//end of $.ajax
    });

我的 WCF 服务就像...

     [OperationContract]
            [WebInvoke(Method = "POST",
                UriTemplate = "AuthorPostByJson", 
                ResponseFormat = WebMessageFormat.Json, 
                RequestFormat = WebMessageFormat.Json)]       
            List<Book> GetBooksByAuthor_JSON(Author author);

我的 web.config 文件....

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="Platform.WebRestful.Service1Behavior"
        name="Platform.WebRestful.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="Platform.WebRestful.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
      <service behaviorConfiguration="Platform.WebRestful.BookServiceHostRestfulBehavior"
        name="Platform.WebRestful.BookServiceHostRestful">
        <endpoint address="" binding="webHttpBinding" contract="Platform.WebRestful.IBookServiceHostRestful">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Platform.WebRestful.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="Platform.WebRestful.BookServiceHostRestfulBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
   <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
4

1 回答 1

1

最后我在一些文章中找到了答案。他们说,在任何跨域(站点)HTTP 请求中,第一个浏览器将发送称为“预检请求”的“OPTIONS”请求......“预检”请求首先将 HTTP OPTIONS 请求标头发送到另一个域上的资源,在为了确定实际请求是否可以安全发送,并且此请求需要适当的标头,表明该服务允许作为响应访问该服务

为此,我们有两个解决方案... 1) WCF 自定义行为 2) 修改 Global.asax 文件的 Application_BeginRequest 事件。

我跟着第二个...解决方案是在WCf服务项目中添加一个Global.asax文件并添加

following code in that,then it perfectly works across any browser...

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
于 2013-10-04T12:36:09.003 回答