简体   繁体   English

角度的CORS错误

[英]CORS Error with Angular

This may be a duplicate question but I have combed stackoverflow and haven't been able to resolve this issue. 这可能是一个重复的问题,但我已经梳理了stackoverflow,但无法解决此问题。 We have a custom back end in c# and front end in angularjs trying to make some http requests (GET, POST, PUTT, DELETE). 我们在c#中有一个自定义的后端,在angularjs中有一个前端,试图发出一些http请求(GET,POST,PUTT,DELETE)。 We have the back end running on port 33805 and front end on 8080. I've verified that the actual code is fine. 我们的后端在端口33805上运行,前端在8080上运行。我已验证实际代码是可以的。 The GET calls work fine. GET调用工作正常。 Its when I try doing other calls that we get the issue. 当我尝试拨打其他电话时,我们遇到了问题。 Here is the header for the request of a POST: 这是POST请求的标头:

OPTIONS /api/users/HTTP/1.1
**Client**
    Accept: */*
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8 User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrone/44.0.2403.107 Safari/537.36
**Miscellaneous**
    Access-Control-Request-Headers: accept, content-type
    Access-Control-Request-Method: POST
    Origin: http://localhost:8080
    Referer: http://localhost:8080/VM/?newPassword=NewPassword3&rePassword=NewPassword3
**Transport**
    Connection: keep-alive
    Host: localhost:33805

Here is the header for the response: 这是响应的标题:

HTTP/1.1 405 Method Not Allowed
**Cache**
    Cache-Control: no-cache
    Date: Tue, 04 Aug 2015 16:00:31 GMT
    Expires: -1
    Pragma: no-cache
**Entity**
    Content-Length: 76
    Content-Type: application/json; charset=utf-8
**Miscellaneous**
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZGVyaWNrM1xEb2N1bWVudHNcd29ya3NwYWNlXFZlbmRvck1hbmFnZW1lbnRTZXJ2aWNlc1xWZW5kb3JNYW5hZ2VtZW50U2VydmljZXNcYXBpXHVzZXJzXA==?=
**Security**
    Access-Control-Allow-Headers: AUTHORIZATION, X-CLIENT-ID, X-CLIENT_SECRET
    Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
    Access-Control-Allow-Origin: *

I am simply trying to add a user to the system. 我只是想将用户添加到系统中。 If it helps the error I'm getting in the console on chrome reads: 如果它可以帮助我在chrome的控制台中读取错误,则显示为:

XMLHttpRequest cannot load http://localhost:33805/api/users/ . XMLHttpRequest无法加载http:// localhost:33805 / api / users / Invalid HTTP status code 405 无效的HTTP状态代码405

Again I know there are many questions about CORS and setting it up and what not but I feel like I've followed them all and still am not sure whats going on. 再一次,我知道关于CORS和设置它有很多问题,什么不是,但我觉得我已经全部关注了,但仍不确定发生了什么。 Thanks 谢谢

Can you make your server return the below header and try again? 您能否让服务器返回以下标头,然后重试?

Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept

Your server do supports CORS as it returns below header. 您的服务器确实支持CORS,因为它在标头下面返回。

Access-Control-Allow-Origin: *

So seems like the issue is with allowed-headers returned by the server 所以看起来问题出在服务器返回的允许标题

You will have to allow it on the server side. 您将不得不在服务器端允许它。 In IIS7 you can do this: (add this in your apps web.config) 在IIS7中,您可以执行以下操作:(将其添加到您的应用程序web.config中)

<customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
 </customHeaders>

In Tomcat you can add cors filter in web.xml. 在Tomcat中,您可以在web.xml中添加cors过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM