2

this is my controller:

public function index(Request $request)
    {
        $items = Post::latest()->paginate(5);
        $cmnt = Comment::all();

        return response()->json(array('posts'=>$items,'comment'=>$cmnt));
    }

this my ajax request

function getPageData() {
      $.ajax({
        dataType: 'json',
        url: "{{route('post_status.index')}}",
        data: {page:1}
      }).done(function(data){
            manageRow(data.data);
      });
    }
function manageRow(data) {
          console.log(data.comment);
}

why i am getting error ?? help me out of this plzz

4
  • "why i am getting error" What error? Commented Nov 10, 2016 at 21:36
  • actually it shows me nothing.....no value at all Commented Nov 10, 2016 at 21:38
  • @leo have you tried looking in the console? Also in your comment on the answer you say something about multiple objects? Could you actually explain what you are trying to do in your question? Commented Nov 11, 2016 at 5:19
  • i want to return $items and $cmnt this two object from my controller using ajax response Commented Nov 11, 2016 at 15:24

2 Answers 2

6

laravel returns json by default if it doesn't return view, in your case index() should return:

return ['posts' => $items,'comment' => $cmnt];

also I don't think this is correct

{{route('post_status.index')}}

probably should be

{{ url('post_status/index') }}
Sign up to request clarification or add additional context in comments.

1 Comment

route is ok when i return one object it works well but when i want to return multiple object it doesn't work
0

You can use the type of response from the server.

function getPageData() {
    $.ajax({
      dataType: 'json',
      url: "{{route('post_status.index')}}",
      type: 'GET'
      data: {page:1}
    }).done(function(data){
        manageRow(data.data);
    });
}

function manageRow(data) {
    console.log(data.comment);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.