PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] How can I return view and pass in data in Laravel with Ajax?

 September 06, 2022     ajax, javascript, laravel, php, view     No comments   

Issue

I have an issue with my Ajax function which is supposed to call a Laravel view with $paginatedResults data which is returns from another function. For some reason it returns error 500. I know for a fact that $paginatedResults returns the right data as if I var_dump it's correct.

public function ajaxSearch(Request $request)
{
    $data = $request->input('searchVal');
    $pageNr = $request->input('pageNr');
    $dataType = $request->input('dataType');
    $paginatedResults = $this->paginateTypeResults($data, $pageNr, $filetype);

    $loadData = view('search.searchresults')->with($paginatedResults)->render();
    echo response()->json($loadData);
}

Solution

This is json response

return response()->json([
   'html' => view('search.searchresults', ['paginatedResults' => $paginatedResults])->render()
]);

After success

$.ajax({
   //
   dataType: 'json',
   success: function(response) {
      $('.//here').html(response.html)
   }
})


Answered By - フクちゃん
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing