mardi 25 juillet 2017

How to: AngularJS http GET request to Laravel CRUD resource controller

I have been trying to get a response from my laravel resource controller through a http GET request from angularJS. Please let me know what i'm doing wrong.

I have a laravel resource(CRUD) controller named CommentsController which has the function.

public function show(Comment $comment) {
    //show comement
    $articleComments = DB::table('comments')->where('id', $comment->articleID);
    if (!$articleComments->isEmpty()) {
        //return Json as response
        return response()->json($articleComments);
    } else {
        return response()->json(['status' => 'Not Found!']);
    }
}

I want to use AngularJS http GET request to retrieve comments for an article. This is my AngularJS code.

var app = angular.module('myApp', []);
      app.controller('myCtrl', function($scope, $http) {
          $scope.loadComments = function(id){
            $http.get("comments",{
              params: {
                'articleID': id
              }})
            .then(function(response) {
                //First function handles success
                console.log(response.data)
            }, function(response) {
                //Second function handles error
                console.log(response.data)
            });
          }
      });

I have included the CSRF protection tag as well

<meta name="csrf-token" content="">

My response looks like

Chrome network analysis header output



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire