2016-03-08 70 views
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'; 
    }); 

回答

1

检查,如果你的表单操作的网址是正确的,如果您使用的是组前缀你的路线,不要忘记把它列入表单操作URL

0

可能是你将不得不先设定的角度模块中的报头。然后你会发出http请求。

var app = angular.module('App', [], ['$httpProvider', function($httpProvider) { 
    //Setting headers 
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 
    $httpProvider.defaults.headers.common['X-Requested-With'] = "XMLHttpRequest"; 
    $httpProvider.defaults.headers.post['X-CSRF-TOKEN'] = $('meta[name=_token]').attr('content'); 
}]); 

app.controller('main',['$scope','$http','$httpParamSerializerJQLike',function($scope,$http,$httpParamSerializerJQLike) { 
$scope.courses = []; 
    $http({ 
    url: "/afil", 
    method: "POST", 
    data: $httpParamSerializerJQLike() 
}).success(function(res) { 
    console.log(res); 
    // $scope.courses = res; 
}).error(function(res) { 
}); 
}]); 

希望这会帮助你。

+0

没有工作 – Burawi

+0

@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) –