问题
当我尝试发布的数据,我得到的错误。当然,我做错了什么,也是即时新的角度。当我做得很好,但我不知道如何做POST。
控制器
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', '[email protected]');
Route::post('lists', '[email protected]');
});
Laravel中间件
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,
];
}
尝试与
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');
})
}
}]);
解决,但它不工作了。我不知道我做错了,如何发布数据的权利..
错误消息从控制台
错误:XMLHttpRequest的无法加载本地主机:8888 /列表。请求的资源上没有“Access-Control-Allow-Origin”标题。 Origin'localhost';因此不被允许访问。
问题
如何解决这个错误,并得到角JS $ HTTP POST与laravel?
我的意见
我觉得有什么不对的角度控制器,它不能从形式或类似的东西,也许数据。
你得到的错误是什么? –
只是说,你应该把你的实际http请求放在服务/工厂中,然后在你的控制器中调用它 –
控制台出错: XMLHttpRequest无法加载http:// localhost:8888/lists。请求的资源上没有“Access-Control-Allow-Origin”标题。因此不允许Origin'http:// localhost'访问。 false – rincuuk