laravel ajax 404 页面未找到

laravel ajax 404 page not found

尝试在 Laravel 中使用 AngularJS 发送 post 请求,我收到此错误消息:

Sorry, the page you are looking for could not be found.

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';
  });

可能您必须先在 angular 模块中设置 header。然后你会发出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) {
});
}]);

希望对您有所帮助。

检查您的表单操作 url 是否正确,如果您在路由中使用组前缀,请不要忘记将其包含在表单操作中 url