流明:启用CORS
Lumen: Enable CORS
我用Lumen构建了一个API,并希望用JavaScript和XMLHttpRequest对象访问它。但每次我的PUT、GET、POST和DELETE请求都被转换为OPTIONS-Request。我读了很多关于CORS信息的网站。我用以下内容构建中间件:
class CORSMiddleware
{
public function handle($request, 'Closure $next)
{
$response = null;
/* Preflight handle */
if ($request->isMethod('OPTIONS')) {
$response = new Response();
} else {
$response = $next($request);
}
$response->header('Access-Control-Allow-Methods', 'OPTIONS, HEAD, GET, POST, PUT, DELETE');
$response->header('Access-Control-Allow-Headers', $request->header('Access-Control-Request-Headers'));
$response->header('Access-Control-Allow-Origin', '*');
return $response;
}
}
我的客户代码:
var url = "http://localhost:8000/api/user";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open('PUT', url, false);
xmlHttpRequest.send('{"username": "ABC", "password": "ABC","email": "mail@cool.xyz" }');
if (xmlHttpRequest.status == 200) {
console.log(xmlHttpRequest.responseText);
}
我的GET请求信息:
Host: localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Origin: null
Connection: keep-alive
Cache-Control: max-age=0
我的GET请求响应信息:
Access-Control-Allow-Methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Connection: close
Content-Type: text/html; charset=UTF-8
Date: Sun, 27 Dec 2015 10:36:51 GMT
Host: localhost:8000
x-powered-by: PHP/7.0.0
PUT请求的我的请求信息:
Host: localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: PUT
Origin: null
Connection: keep-alive
Cache-Control: max-age=0
我的PUT请求的响应信息:
Cache-Control: no-cache
Connection: close
Content-Type: text/html; charset=UTF-8
Date: Sun, 27 Dec 2015 10:36:51 GMT
Host: localhost:8000
x-powered-by: PHP/7.0.0
飞行前没有"Access Control Allow-*"-标头。我不知道为什么;我用我的lume-cors中间件启用了它。
将以下标头添加到公共/.htaccess文件中。
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
Header set Access-Control-Allow-Credentials "true"
这对于解决跨来源问题非常有效。
在Lumen
中,需要为每个POST, PUT, DELETE...
路由手动设置OPTIONS
路由。
这就是我为解决问题所做的。
$app->options('{all:.*}', ['middleware' => 'cors.options', function() {
return response('');
}]);
上面的路由将为您捕获所有OPTIONS
请求。
在cors.options
中间件中:
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN'])
->header('Access-Control-Allow-Methods', 'PUT, POST, DELETE')
->header('Access-Control-Allow-Headers', 'Accept, Content-Type,X-CSRF-TOKEN')
->header('Access-Control-Allow-Credentials', 'true');
}
相关文章:
- 在Golang回调函数中启用CORS
- 如何在angular js中启用CORS
- Elasticsearch https cors已启用,但仍获得No'访问控制允许来源'标头存在于请求的
- 在JQuery中启用CORS后,CORS请求被阻止
- 如何在OrientDb-server-config.xml中启用CORS
- 无法理解如何使用 ajax 调用启用 CORS
- 在角度全栈生成器上启用 CORS
- 在 Firefox 中启用 Cors
- 如何在Dynamics CRM中启用CORS
- 在NodeJS中启用CORS
- 启用CORS时的安全漏洞
- 启用来自角度JS的控制器的CORS
- 流明:启用CORS
- 在API上启用CORS
- 如何启用CORS(JS+MVC Web API)
- 如何在谷歌驱动器上托管的文件夹中启用CORS
- 在Sharepoint 2013中启用CORS
- 是否有方法可以快速确定浏览器是否支持启用了cors的图像而不会污染浏览器?
- 快速启用CORs标头仍然会导致错误
- 在WebAPI上启用CORS, Chrome和FF仍然不起作用