Response to preflight request doesn't pass access control check

后端 未结 21 2186
别那么骄傲
别那么骄傲 2020-11-22 03:50

I\'m getting this error using ngResource to call a REST API on Amazon Web Services:

XMLHttpRequest cannot load http://server.apiurl.com:8000/s/login

相关标签:
21条回答
  • 2020-11-22 04:27

    Something that is very easy to miss...

    IN solution explorer, right-click api-project. In properties window set 'Anonymous Authentication' to Enabled !!!

    0 讨论(0)
  • 2020-11-22 04:28

    You are running into CORS issues.

    There are several ways to fix/workaround this.

    1. Turn off CORS. For example: how to turn off cors in chrome
    2. Use a plugin for your browser
    3. Use a proxy such as nginx. example of how to set up
    4. Go through the necessary setup for your server. This is more a factor of the web server you have loaded on your EC2 instance (presuming this is what you mean by "Amazon web service"). For your specific server you can refer to the enable CORS website.

    More verbosely, you are trying to access api.serverurl.com from localhost. This is the exact definition of cross domain request.

    By either turning it off just to get your work done (OK, put poor security for you if you visit other sites and just kicks the can down the road) you can use a proxy which makes your browser think all requests come from local host when really you have local server that then calls the remote server.

    so api.serverurl.com might become localhost:8000/api and your local nginx or other proxy will send to the correct destination.


    Now by popular demand, 100% more CORS info....same great taste!


    And for the downvoters.... bypassing CORS is exactly what is shown for those simply learning the front end. https://codecraft.tv/courses/angular/http/http-with-promises/

    0 讨论(0)
  • 2020-11-22 04:29

    In my Apache VirtualHost config file, I have added following lines :

    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
    Header always set Access-Control-Max-Age "1000"
    Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
    
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteRule ^(.*)$ $1 [R=200,L]
    
    0 讨论(0)
  • 2020-11-22 04:31

    Our team occasionally sees this using Vue, axios and a C# WebApi. Adding a route attribute on the endpoint you're trying to hit fixes it for us.

    [Route("ControllerName/Endpoint")]
    [HttpOptions, HttpPost]
    public IHttpActionResult Endpoint() { }
    
    0 讨论(0)
  • 2020-11-22 04:32

    A very common cause of this error could be that the host API had mapped the request to a http method (e.g. PUT) and the API client is calling the API using a different http method (e.g. POST or GET)

    0 讨论(0)
  • 2020-11-22 04:34

    In PHP you can add the headers:

    <?php
    header ("Access-Control-Allow-Origin: *");
    header ("Access-Control-Expose-Headers: Content-Length, X-JSON");
    header ("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
    header ("Access-Control-Allow-Headers: *");
    ...
    
    0 讨论(0)
提交回复
热议问题