I am new to slim 3 and I’m trying to get cors working with a preflight request. It works when a preflight request is not sent, but fails when sending the preflight. Best I can tell the options route is not working. I have copied the code from the Doc to implement but have had no luck. I get this error in the console
Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
These are the headers I get
Request URL
Request Method:OPTIONS
Status Code:200 OK
Remote Address:75.150.65.141:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
HTTP/1.1 200 OK
Allow: OPTIONS, TRACE, GET, HEAD, POST
Server: Microsoft-IIS/7.5
Public: OPTIONS, TRACE, GET, HEAD, POST
X-Powered-By: ASP.NET
Date: Thu, 09 Nov 2017 16:16:20 GMT
Content-Length: 0
X-Cache: MISS from barracudaXXXXX
Via: 1.1 XXXXX (http_scan_byf/3.5.16)
Connection: keep-alive
Request Headers
OPTIONS /test/rob HTTP/1.1
Host:
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: GET
Origin: my.skydivect.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Access-Control-Request-Headers: x-requested-with
Accept: /
Referer:
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Here is the code I have implemented.
$app->options(’/{routes:.+}’, function ($request, $response, $args) {
$this->logger->addInfo(“Options run”);
return $response;
});
$app->add(function (Request $req, Response $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’)
->withHeader(‘Content-Type’,‘application/json’);
});
$app->get(’/test/{name}’, function (Request $request, Response $response){
$name = $request->getAttribute(‘name’);
$response->getBody()->write(“Hello, $name”);
return $response;
});
$app->run();
Thanks for the help