Response to preflight request doesn't pass access control check

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

    Disable the chrome security.Create a chrome shortcut right click -> properties -> target, paste this "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="c:/chromedev"

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

    To fix cross-origin-requests issues in a Node JS application:

    npm i cors
    

    And simply add the lines below to the app.js

    let cors = require('cors')
    app.use(cors())
    
    0 讨论(0)
  • 2020-11-22 04:39

    I think disabling CORS from Chrome is not good way, because if you are using it in ionic, certainly in Mobile Build the Issue will raise Again.

    So better to Fix in your Backend.

    First of all In header, you need to set-

    • header('Access-Control-Allow-Origin: *');
    • header('Header set Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"');

    And if API is behaving as GET and POST both then also Set in your header-

    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) header("Access-Control-Allow-Headers:
    {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); exit(0); }

    0 讨论(0)
提交回复
热议问题