问题描述:

So I'm using an AngularJS front end to communicate with a Slim PHP api backend, but everytime I make an $http request, I get the following error:

XMLHttpRequest cannot load http://.... Request header field

Content-Type is not allowed by Access-Control-Allow-Headers in

preflight response.

I'm not sure how to fix the issue, as my google search doesn't seem to lead me anywhere. Below is my Slim Php api main file, where I would then include my different api routes, like Login.

Slim Php

use \Psr\Http\Message\ServerRequestInterface as Request;

use \Psr\Http\Message\ResponseInterface as Response;

require 'vendor/autoload.php';

$app = new \Slim\App;

$app->options('/{routes:.+}', function ($request, $response, $args) {

return $response;

});

$app->add(function ($req, $res, $next) {

$response = $next($req, $res);

return $response

->withHeader('Access-Control-Allow-Origin', '*')

->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')

->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

});

require_once 'api/login.php';

$app->run();

Also, I have an .htaccess file, and my code for that is below.

<IfModule mod_headers.c>

Header set Access-Control-Allow-Origin "*"

</IfModule>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^ index.php [QSA,L]

My Angular front end exists in a public folder, and my Slim Php exists in it's own server folder. I'm not sure what the solution is, and would love to get your ideas on how I might be able to solve this issue. Thank you everyone in advance.

相关阅读:
Top