0

This question has been asked here: How can I return a view from an AJAX call in Laravel 5? And a few other questions. The answers are a mixed bag of responses with top answers not working for me. I'll explain all below.

I currently have an Ajax call returning a successful response when I do this:

    public function search (Request $request)
    {
    $output =             echo '<td>Notes</td>';
                echo '<td colspan="8" id="orderNotes"><input type="text" name="notes" style="width: 100%;"></td>';
                echo '<td><button class="btn btn-info btn-fill btn-sm" id="saveNotes" data-orderNumber="';

// a bunch of more echos

    return Response($output); 

    }

This causes a bunch of problems for me because I'm manually typing out each echo. I would instead like to replace it by returning a view.

From the answered above it would appear that this would be the correct answer:

public function search(Request $request)

{
    $title = "just testing";
    $view = view("pages.orders-display",compact('title'))->render();
    return response()->json(['html'=>$view]);
}

I created a view called orders-display. The path is views/pages/orders-display.blade.php this gives me an internal 500 error.

The above answer I pointed out has this as the highest agreed upon answer but it also doesn't work for me.

public function search(Request $request)

{

  $userjobs = 'this is a test';

  $returnHTML = view('pages.orders-display')->with('userjobs', $userjobs)->render();
  return response()->json(array('success' => true, 'html'=>$returnHTML));

}

Any ideas what I'm doing wrong?

FabricioG
  • 3,107
  • 6
  • 35
  • 74

1 Answers1

0

They are not working for you because they return a JSON response and not an html one. You should return the view directly, without the render method call.

Try this

return view('pages.your_page');

or

return view('pages.your_page')->render();
  • 500 error... But remember it's being returned from an Ajax call @Laurentiu Turcu – FabricioG Aug 12 '19 at 20:38
  • @FabricioG That should not result in a 500 error, neither the solutions in your question should. You should enable the debug mode of your app to trace the error, and post it here in order to help you. – Laurentiu Turcu Aug 12 '19 at 20:46
  • According to this: https://stackoverflow.com/questions/38475451/ajax-laravel-return-view-500-error. you shouldn't return a view in an Ajax request. It would cause a 500 error @Laurentiu Turcu – FabricioG Aug 12 '19 at 20:54
  • @FabricioG you tried to return a JSON in the second and third solution in your question and you still got a 500... there is another problem in your app. I just tried returning a view in an API route and it works just fine. `Route::get('/test', function(Request $request) { return view('welcome'); });` – Laurentiu Turcu Aug 12 '19 at 21:05
  • You are correct, I believe the problem is with the view itself. Network error is showing this: "message": "View [.pages.order-display.blade.php] not found.", "exception": "InvalidArgumentException", @Laurentiu Turcu digging into it more enow. – FabricioG Aug 12 '19 at 21:08