Response to preflight request doesn't pass access control check

后端 未结 21 2184
别那么骄傲
别那么骄傲 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:15

    I have faced with this problem when DNS server was set to 8.8.8.8 (google's). Actually, the problem was in router, my application tried to connect with server through the google, not locally (for my particular case). I have removed 8.8.8.8 and this solved the issue. I know that this issues solved by CORS settings, but maybe someone will have the same trouble as me

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

    My "API Server" is an PHP Application so to solve this problem I found the below solution to work:

    Place the lines in index.php

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
    
    0 讨论(0)
  • 2020-11-22 04:17

    For those are using Lambda Integrated Proxy with API Gateway. You need configure your lambda function as if you are submitting your requests to it directly, meaning the function should set up the response headers properly. (If you are using custom lambda functions, this will be handled by the API Gateway.)

    //In your lambda's index.handler():
    exports.handler = (event, context, callback) => {
         //on success:
         callback(null, {
               statusCode: 200,
               headers: {
                    "Access-Control-Allow-Origin" : "*"
               }
         }
    }
    
    0 讨论(0)
  • 2020-11-22 04:18

    For python flask server, you can use the flask-cors plugin to enable cross domain requests.

    See : https://flask-cors.readthedocs.io/en/latest/

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

    It's easy to solve this issue just with few steps easily,without worrying about anything. Kindly,Follow the steps to solve it .

    1. open (https://www.npmjs.com/package/cors#enabling-cors-pre-flight)
    2. go to installation and copy the command npm install cors to install via node terminal
    3. go to Simple Usage (Enable All CORS Requests) by scrolling.then copy and paste the complete declartion in ur project and run it...that will work for sure.. copy the comment code and paste in ur app.js or any other project and give a try ..this will work.this will unlock every cross origin resource sharing..so we can switch between serves for your use
    0 讨论(0)
  • 2020-11-22 04:20

    If you're writing a chrome-extension

    You have to add in the manifest.json the permissions for your domain(s).

    "permissions": [
       "http://example.com/*",
       "https://example.com/*"
    ]
    
    0 讨论(0)
提交回复
热议问题