简体   繁体   中英

laravel ajax 404 page not found

trying to send post request using AngularJS in Laravel, I'm having this error message:

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

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

Might be you will have to first set the header in angular module. Then you will make the http request.

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) {
});
}]);

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM