0

Before I use AJAX, the controller will return a "index" view with value filled,

return view('index')->with(
    'languages' => $languages);

But when I am trying to use AJAX, I only got the webpage in Response. How can I render the HTML page now? I am not sure if I've put it clear. Thanks.

John Mccandles
  • 1,181
  • 2
  • 16
  • 24
  • 1
    http://stackoverflow.com/questions/22068002/laravel-4-ajax-with-partial-views , http://stackoverflow.com/questions/15096241/laravel-how-to-render-only-one-section-of-a-template , http://stackoverflow.com/questions/24984073/laravel-rendering-partial-views-with-ajax-requests Check if this will help you – Anup Yadav Aug 17 '15 at 13:48

2 Answers2

2

You have to render the content in a html element for example.

<div id="content"></div>

then you can render it in AJAX success

 $.ajax({
            type: 'get',
            url: "your url",
            dataType: 'html',

            success: function(content) {
                $("#content").html(content);

            },
            error: function(xhr, status, text) {
                $("#content").text(text);
            }
        });

Hope this helps.

Hasan Tareque
  • 1,761
  • 11
  • 21
0

Thanks for @hmtareque and @anup-yadav 's help. I finally get it through. I changed my controller from

return view('index')->with(
    'languages' => $languages);

to

  return response()->json(array('success' => true, 'data_generated'=>$data_generated));

And in JS, I used

 $.ajax({
        type: 'get',
        url: "your url",
        dataType: 'json',
        success: function(data) {
            $("#content").val(data.data_generated);
        },
    });

Thanks again for the help.

John Mccandles
  • 1,181
  • 2
  • 16
  • 24