预检响应具有无效的 HTTP 状态代码 405 [英] Response for preflight has invalid HTTP status code 405

查看:36
本文介绍了预检响应具有无效的 HTTP 状态代码 405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 StackOverflow 中阅读了许多类似的问题,但这些解决方案对我不起作用.

我有 WCF REST 服务:

[][<WebInvoke(UriTemplate = "PostItem",RequestFormat= WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, Method = "POST")>]

我可以使用 Postman(Chrome 扩展程序)来使用它.我将数据作为原始"而不是urlencoded"传递.我得到了 200 个返回码.

我需要使用 angularjs 调用这个方法:

 $http.post('http://192.168.1.65/Service1.svc/restapi/PostItem',{"Address": "г. Москва, ул. Соколово-Мещерская, д.25",...用户":"})

我刚刚从 Postman 复制了 URL 和 JSON.但我收到错误:

<块引用>

angular.js:10722 选项

评论和回复标签为空

更新 2:

在 IE 中一切正常,但在 Chrome 中不起作用.

解决方案

看起来我找到了解决方案.我刚刚添加了第二种方法:

[][<WebInvoke(UriTemplate = "PostTest",RequestFormat= WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, Method = "POST")>]抽象 PostTest: obj: 测试 ->单元[<运营合同>][<WebInvoke(UriTemplate = "PostTest",RequestFormat= WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, Method = "OPTIONS")>]抽象 PostTestOptions: unit ->单元

它只是什么都不做的空方法.我不知道原因,但一切正常.

I have read many similar problems in StackOverflow, but the solutions doesn't work for me.

I have WCF REST service:

[<OperationContract>]    
    [<WebInvoke(UriTemplate = "PostItem", 
            RequestFormat= WebMessageFormat.Json,   
            ResponseFormat = WebMessageFormat.Json, Method = "POST")>]         

I can use it using Postman (Chrome extension). I am passing data as 'raw', not 'urlencoded'. And I get 200 return code.

I need to call this method using angularjs:

    $http.post('http://192.168.1.65/Service1.svc/restapi/PostItem',                   
                {
    "Address": "г. Москва, ул. Соколово-Мещерская, д.25",
     ...
    "User": ""
      })  

I have just copied URL and JSON from Postman. But I get the error:

angular.js:10722 OPTIONS http://192.168.1.65/Service1.svc/restapi/PostItem http://192.168.1.65/Service1.svc/restapi/PostItem. Response for preflight has invalid HTTP status code 405

I have searched similar problems and have found two solutions:

  1. Use jQuery to set header Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', but it doesn't work with my WCF service
  2. Set custom headers in my Web.Config:

    <httpProtocol>
       <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*" />
          <add name="Access-Control-Allow-Headers" value="Content-Type" />
          <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
       </customHeaders>
    </httpProtocol>
    

It doesn't help me. And I am not sure that the reason of the error on the server side. The Postman extension can call this method succesfully.

How can I make the same POST call using AngularJS ?

Update:

Here is OPTIONS request:

Review and Response tabs are empty

Update 2:

All works fine in IE, but doesn't work in Chrome.

解决方案

Looks like I have found solution. I just added second method:

[<OperationContract>]    
[<WebInvoke(UriTemplate = "PostTest", 
        RequestFormat= WebMessageFormat.Json,   
        ResponseFormat = WebMessageFormat.Json, Method = "POST")>]         
abstract PostTest: obj: Test -> unit

[<OperationContract>]    
[<WebInvoke(UriTemplate = "PostTest", 
        RequestFormat= WebMessageFormat.Json,   
        ResponseFormat = WebMessageFormat.Json, Method = "OPTIONS")>]         
abstract PostTestOptions: unit -> unit

It is just empty methods that do nothing. I don't know the reason, but all is working.

这篇关于预检响应具有无效的 HTTP 状态代码 405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆