Angularjs将参数数组发送到laravel GET路由

时间:2016-06-04 08:04:12

标签: angularjs laravel-5 laravel-routing

我刚刚从Django迁移到Laravel。 我不确定如何通过我从Angularjs的AJAX请求向Laravel中的GET方法发送可变数量的参数。

我在列表中有一定数量的过滤器,我只想发送未定义的过滤器及其密钥,以便我可以直接执行此操作:

Group::where($filters)->get();

$ filters是我打算通过Angularjs发送的数组,类似于

['group_id'=>101,'Country'=>'India']

或者它需要什么语法。

另外,我如何在route.php的get路径中指定它?

修改

我知道我总是可以使用可选参数,如果没有,但必须有更好的方法,不是吗?

1 个答案:

答案 0 :(得分:1)

1)。 Angular $ http获取请求。

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<p>Today's welcome message is:</p>

<h1>{{myWelcome}}</h1>

</div>

<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome" variable.</p>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $http({
    method : "GET",
    url : "{! URL::to('/project/project-data') !}"
  }).then(function mySucces(response) {
      $scope.myWelcome = response.data;
    }, function myError(response) {
      $scope.myWelcome = response.statusText;
  });
});
</script>

</body>
</html>

2)。为ajax请求创建控制器。

   namespace App\Http\Controllers;

    class ProjectController extends BaseController {

        public function ProjectData(){

            echo "your project data placed here";
            exit()
        }
    }

3)。在routes.php中定义路由URL

Route::get('project/project-data', 'ProjectController@ProjectData');