jdesrosiers / silex-cors-provider

A silex service provider that adds CORS services to silex
MIT License
78 stars 25 forks source link

Show a full app example. #4

Closed corpulent closed 11 years ago

corpulent commented 11 years ago

I tried to get it to work on a simple silex app, it doesn't work.

<?php
require_once __DIR__.'/../vendor/autoload.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag;
use JDesrosiers\Silex\Provider\CorsServiceProvider;

$app = new Silex\Application();
$app->register(new CorsServiceProvider());

$app->before(function (Request $request) {
    if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
        $data = json_decode($request->getContent(), true);
        $request->request->replace(is_array($data) ? $data : array());
    }
});

$app->get('/', function () use ($app) {
    $return = array('hi');
    return $app->json($return, 200);
});

$app->post('/test', function (Request $request) use ($app) {
        return $app->json('success', 200);
 });

$app->after($app["cors"]);

$app->run();
jdesrosiers commented 11 years ago

You are using the service correctly. I looked into it and found a bug in the service. If you had specified the cors.allowOrigin parameter, it would have worked. If you don't set the cors.allowOrigin parameter it is supposed to allow all origins, which I'm sure is what you expected. I just committed the bug fix https://github.com/jdesrosiers/silex-cors-provider/commit/ca3f6ea1c1ca73620501ab164f492692ff11de9d. Thanks for the feedback!

corpulent commented 10 years ago

@jdesrosiers Its still not working. Getting the No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

Here is my composer.json, in cae it might help.

{
    "name": "fabpot/silex-skeleton"
    , "description": "A pre-configured skeleton for the Silex microframework"
    , "license": "MIT"
    , "type": "project"
    , "minimum-stability": "dev"
    , "repositories": [
        {
            "type": "vcs",
            "url": "git@github.com:agwntr/silex-simpleuser.git"
        }
    ]
    , "require": {
        "php": ">=5.3.3"
        , "silex/silex": "~1.0"
        , "silex/web-profiler": "~1.0"
        , "symfony/browser-kit": "~2.3"
        , "symfony/class-loader": "~2.3"
        , "symfony/config": "~2.3"
        , "symfony/console": "~2.3"
        , "symfony/css-selector": "~2.3"
        , "symfony/debug": "~2.3"
        , "symfony/finder": "~2.3"
        , "symfony/form": "~2.3"
        , "symfony/monolog-bridge": "~2.3"
        , "symfony/process": "~2.3"
        , "symfony/security": "~2.3"
        , "symfony/translation": "~2.3"
        , "symfony/twig-bridge": "~2.3"
        , "symfony/validator": "~2.3"
        , "doctrine/dbal": ">=2.2.0"
        , "agwntr/silex-simpleuser": "dev-master"
        , "jdesrosiers/silex-cors-provider": "~0.1"
    }
    , "autoload": {
        "psr-0": { "": "src/" }
    }
    , "extra": {
        "branch-alias": {
            "dev-master": "1.1.x-dev"
        }
    }
}

The app.js is the same as in my original issue. I also tried to add cors.allowOrigin, like this:

$app->register(new CorsServiceProvider(), array(
    "cors.allowOrigin" => "*",
));

This is my ajax query.

request = $.ajax({
                url: 'http://mydomain.com/test,
                    type: 'post',
                    data: JSON.stringify(data),
                    contentType:"application/json",
                    dataType:"json"
                });

My (working) work around so far has been to just do this:

$app->after(function (Request $request, Response $response) {
    $response->headers->set('Access-Control-Allow-Origin', '*');
    $response->headers->set('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Origin');
    $response->headers->set('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
});
jdesrosiers commented 10 years ago

I just setup a project using your code from your first comment, your composer.json, and your ajax query. I used the php built-in server. I ran the app on localhost:8000 and the html on localhost:8001. It worked as expected in chrome and firefox.

It's possible that there could be an incompatibility with the server you are using or the browser you are using. I have tested on the following

Servers

Browsers

What are using?

corpulent commented 10 years ago

@jdesrosiers Sorry for late responses. Confirmed. It works. It must be one of my servers. Using Nginx, php 5.5