Angular2 to REST WebApi CORS issue

后端 未结 10 1937
太阳男子
太阳男子 2020-12-15 06:50

I\'m using an angular2 front end and WebApi backend. The webapi is CORS enabled

var cors = new EnableCorsAttribute(\"*\", \"*\", \"*\");
GlobalConfiguration.         


        
10条回答
  •  执念已碎
    2020-12-15 07:10

    Your error message tells that there is no Access-Control-Allow-Origin in the response of your call. It's something necessary to enable CORS for this request. It's not something related to Angular2.

    This is triggered on the client side by the adding of the Origin header in the request. Do you have this header in your request? Do you use preflighted requests in your other applications. As a reminder:

    • Simple requests. This use case applies if we use HTTP GET, HEAD and POST methods. In the case of POST methods, only content types with the following values are supported: text/plain, application/x-www-form-urlencoded and multipart/form-data.
    • Preflighted requests. When the "simple requests" use case doesn't apply, a first request (with the HTTP OPTIONS method) is made to check what can be done in the context of cross-domain requests.

    Perhaps OPTIONS requests aren't correctly handled on the server side (don't return correct headers, ...).

    What would be interested is to tell us on which requests the error occurs: the OPTIONS one or the target request. You can have a look at the Network tab in DevTools...

    See these links for more details about how CORS works:

    • http://restlet.com/blog/2015/12/15/understanding-and-using-cors/
    • http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#enable-cors

    Hope it helps you, Thierry

提交回复
热议问题