I searched far and wide to solve this problem.
I have an AngularJS web application with Laravel 4 firewall support as follows:
http://app.mydomain.io/ = AngularJS web app
http://api.mydomain.io/ = Laravel Back-end
In 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 setup for an entry 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!";
}
});
In AngularJS, I have an AuthService that looks like this:
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
If I change the Laravel route and the AngularJS service to use GET, everything will work as expected. The problem is with AngularJS.save () creating an OPTIONS request instead of POST (I don't quite understand why).
Can someone help me with the right and best solution?
Thanks!