Return view via ajax in laravel

I want to display a view for the user when the user clicks a button using ajax.
This is my code.

// BASE = localhost/project/public/ $('#button').click(function() { $.ajax({ url: BASE + "user/settings", type: 'GET' }) .done(function( data ) { console.log( data ); }); }); 

And my routes.php

 Route::get('user/settings', ' UserController@getSettings '); 

And UserController.php

 public function getSettings(){ return View::make('user.settings'); } 

But this error is output:

 {"error":{"type":"ErrorException","message":"Undefined offset: 0","file":"H:\\dev \\xampp\\htdocs\\lchat\\vendor\\laravel\\framework\\src\\Illuminate\\Support \\Collection.php","line":470}} 

EDIT: the error was in sight. I fixed it.

Problem 2: the page loaded via ajax itself contains another request to send ajax. but it no longer sends data via ajax. Refreshes the page for sending data.

Jquery code:

 $('#settings :submit').click(function(e){ e.preventDefault(); $.post(BASE + 'settings/save', { 'userName' : $('#userName').val() }, function(data) { return 'OK'; }); }); 

Problem solved: I used .on to bind the event:

 $(document).on('click', '#settings :submit', function(e){ ... } ); 

It works ... Thanks to everyone.

+7
jquery ajax php laravel laravel-4
source share
2 answers

Well, for starters, you have a mistake in your view. The error you get relates to the array you are trying to access that does not actually have the key that you are trying to use ( $arr[0] ).

Once you correct the errors with the view itself, it should work fine. JavaScript will receive the data as HTML, and you can simply paste it wherever you want it to display.

+4
source share

Use string method in front of view file

 return (String) view('Company.allUserAjax'); 
+3
source share

All Articles