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.
jquery ajax php laravel laravel-4
Pars
source share