Problem
When I try to POST data I get error. For sure I do something wrong, also im new at angular. When I do GET all work fine, but Im not sure how to do POST.
Controller
myApp.controller('createListController', ['$scope', '$log', '$http',
function($scope, $log, $http){
$http({
method: 'POST',
url: 'http://localhost:8888/lists',
data: $scope.name
})
}]);
HTML
<form method="post" action="" >
<label>List name</label>
<input type="text" ng-model="name" name="name" class="form-control" placeholder="Type list name" />
<button type="submit" ng-model="submit" name="submit" class="btn btn-primary form-control">Add list</button>
</form>
Laravel rout.php
Route::group(['middleware' => 'cors'], function () {
Route::get('lists', 'ListsController@index');
Route::post('lists', 'ListsController@store');
});
Laravel middleware
class Cors
{
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set(
'Access-Control-Allow-Headers',
'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since'
);
$response->headers->set('Access-Control-Allow-Credentials', 'true');
$response->headers->set('Access-Control-Allow-Methods', '*');
return $response;
}
}
Laravel kernel.php
protected $routeMiddleware = [
'cors' => \App\Http\Middleware\Cors::class,
];
}
Attempted to solve with
myApp.controller('createListController', ['$scope', '$log', '$http',
function($scope, $log, $http){
if ($scope.submit == true){
$http({
method: 'POST',
url: 'http://localhost:8888/lists',
data: {name: 'text'}
})
.success(function () {
console.log('true');
})
.error(function(){
console.log('false');
})
}
}]);
But it's not working too. I don't know what I do wrong and how to post data right..
Error message
Error from console: XMLHttpRequest cannot load localhost:8888/lists. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost'; is therefore not allowed access.
Question
How do I solve this error and get angular js $http post with laravel?
My opinion
I think there is something wrong in angular controller, it can't get data from form or something like that maybe.