阅读了许多问题/答案后,我决定发布。我认为Slim Framework - jQuery $.ajax request - Method DELETE is not allowed by Access-Control-Allow-Methods总结了我发现并尝试过的大多数信息。
我将MAMP与PHP 5.6一起用于开发,但是生产环境很可能是共享主机。我也在用ember.js
当ember发出POST请求时,我收到Access-Cross-Origin消息:
我了解在服务器上设置适当的 header 可以解决此问题,但我不知道何时执行此操作。我目前在Slim框架中所做的是:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
$app->options('/(:name+)', function() use($app) {
$response = $app->response();
$app->response()->status(200);
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, X-authentication, X-client');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
但是,我已经检查了Ember.js请求,它没有请求
OPTIONS
,因此未设置正确的 header 。如果我在Slim的单个路由中设置标题,则它可以正常工作。但是我不想在每条路线上一一设置标题。
如何设置所有路由的标题?
最佳答案
CorsSlim是您的 friend :
<?php
$app = new \Slim\Slim();
$corsOptions = array(
"origin" => "*",
"exposeHeaders" => array("Content-Type", "X-Requested-With", "X-authentication", "X-client"),
"allowMethods" => array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS')
);
$cors = new \CorsSlim\CorsSlim($corsOptions);
$app->add($cors);
关于php - Slim Framework和Ember.js中的Access-Control-Origin,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29337646/