1
试图发送使用AngularJS POST请求在Laravel,我有此错误消息:laravel阿贾克斯的404页
对不起,您要找的页面无法找到。
JAVASCRIPT
app.controller('main',['$scope','$http','$httpParamSerializerJQLike',function($scope,$http,$httpParamSerializerJQLike) {
$scope.courses = [];
$http({
url: "/afil",
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded',"X-Requested-With":"XMLHttpRequest"},
data: $httpParamSerializerJQLike()
}).success(function(res) {
console.log(res);
// $scope.courses = res;
}).error(function(res) {
});
}]);
routes.php文件
Route::post('/afil', function() {
return 'Hello World';
});
没有工作 – Burawi
@Burawi 请注意, : 在进行AJAX调用时,X-Requested-With标头通常设置为XMLHttpRequest。 Laravel的Request :: ajax()方法构建在Symfony2方法的基础上,该方法只是检查是否存在此标头。 2012年10月,Angular.js删除了这个标题,因为他们觉得它很少被使用。 你需要设置X-要求,通过参数的角度 '$ httpProvider.defaults.headers.common [ “X-要求,以”] = “XMLHttpRequest的”' 详情阅读[链接]( https://laracasts.com/discuss/channels/requests/laravel-5-doesnt-behave-to-angularjs-like-ajax-request) –