路由问题与Laravel,AngularJS和CORS [英] Routing issue with Laravel, AngularJS and CORS

查看:192
本文介绍了路由问题与Laravel,AngularJS和CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找远近为解决这个问题。

I have been searching far and wide for a solution to this problem.

我有一个Laravel 4后端实现一个AngularJS的Web应用程序如下:

I have an AngularJS web app with a Laravel 4 backend implementation as follows:

http://app.mydomain.io/ = AngularJS web app
http://api.mydomain.io/ = Laravel Back-end

在在Laravel的routes.php文件文件我有以下PHP code设置Access-Control头:

Within the routes.php file in Laravel I have the following PHP code to set the Access-Control headers:

header('Access-Control-Allow-Origin: http://app.mydomain.io');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');

我也有一个路径设置为登录请求如下:

I also have a route set-up for a login request as follows:

Route::post('/login', function()
{
    $email = Input::get('email');
    $password = Input::get('password');
    if (Auth::attempt(array('email' => $email, 'password' => $password)))
    {
        return "Success!";
    } else {
        return "Fail!";
    }
});

在AngularJS我有一个AuthService看起来象下面这样:

In AngularJS I have a AuthService which looks like below:

app.factory('AuthService', ['$resource', '$q', '$cookieStore', function($resource, $q, $cookieStore) {
    var user = null;
    var Service = $resource('//api.mydomain.io/login/', {}, {});
    return {
        login: function(email, password) {
            var deferred = $q.defer();
            Service.save({email: email, password: password}, function(response) {
                $cookieStore.put('user', JSON.stringify(response));
                deferred.resolve(true);
            }, function(error) {
                deferred.reject(error);
            });
            return deferred.promise;
        }
    };
}]);

当这个请求时,我得到了以下内容:

When this request is made I get the following:

XMLHttpRequest cannot load http://api.mydomain.io/login. Invalid HTTP status code 404

如果我改变Laravel路由和AngularJS服务使用GET,一切正常。这个问题源于AngularJS .save()作出OPTIONS请求,而不是POST(我不完全理解为什么)。

If I change the Laravel route and AngularJS service to use GET, everything works as expected. The problem stems from AngularJS .save() making a OPTIONS request instead of POST (I don't fully understand why).

谁能帮我用正确的和最佳实践的解决方案?

Could anyone help me with the proper and best practice solution?

感谢您!

推荐答案

以下解决方案工作:

在filters.php添加以下内容:

Within filters.php add the following:

App::before(function($request)
{
    if (Request::getMethod() == "OPTIONS") {
        $headers = array(
            'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers'=> 'X-Requested-With, content-type',);
        return Response::make('', 200, $headers);
    }
});

而在routes.php文件的顶部添加以下内容:

And at the top of routes.php add the following:

header('Access-Control-Allow-Origin: http://app.mydomain.io');

由于采用了谷歌加社会! :)

Thanks to the Google Plus community! :)

莱昂。

这篇关于路由问题与Laravel,AngularJS和CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆