Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 jquery Ajax Request

Tags:

ajax

laravel

I am new to Laravel 4. I have a page within which I have 3 tabs(basically big clickable divs) to load the different user information. What I would like to do is that I would like to be able to click on those tabs,make jquery ajax request,bring back a partial view and update a part of page with that partial view. I would like to able to do that for every tab.

I got the click event firing.I also tried different code to accomplish ajax request. For some reason, nothing happens. I do not know if I am missing something in route.php. Could anyone please give me some code on how to do that? or may be some other idea?

I did this::

Route.php

Route::post('userInfo','UserController@showInfo');

jqueryAjax:

$("divtoUpdatePartialView").click(function(){

$.ajax({
  type: "POST",
  url:Not Sure,
  data: { id: userId }
}).done(function( msg ) {
  alert( msg );
});

}

I tried using Base Url too, but nothing happens. I have an ajax library declared on my master page. Could you please help me or give me some other ideas?

Thank you very much for your answer. I tried that way, this is what I have...

Route.php:

Route::get('user_Profile/userInfo', 'UserController@getshow');

The ajax:

var  userId='1';

$.ajax({
    type: "POST",
    url: 'user_Profile/userInfo',
    data: { id: userId }
}).done(function( msg ) {
    alert( "the messageis "+msg );
});


My userController::

public function getshow()
    {
           return "No ajax";
    if (Request::ajax()) {

        return "yeahhhh";
    $html=View::make('user.userProfile')->nest('partial', 'user.userInfoPartial')-        >with('title',$title)->render();


}
}

When I directly access the page I receive "No ajax", but when I try the ajax-way,nothing happens.Could you see what I am missing??

Thank you again for your help..

like image 921
Ranjan Avatar asked Jul 10 '13 17:07

Ranjan


2 Answers

The url should be the path/pattern you have used in your route and in this case it's

userInfo

The ajax

$.ajax({
    type: "POST",
    url: 'userInfo',
    data: { id: userId }
}).done(function( msg ) {
    alert( msg );
});

Also, make sure that, you can directly access the page from your browser's address bar using

http://yourdomain.com/userInfo
like image 66
The Alpha Avatar answered Jan 02 '23 13:01

The Alpha


return sentence should go AFTER the if sentence;

   public function getshow(){

    if (Request::ajax()) {
        $html=View::make('user.userProfile')->nest('partial', 'user.userInfoPartial')-        >with('title',$title)->render();
        return "yeahhhh";
    }
           return "No ajax";
}

Return sentence exits the function, that means that any further code is not going to be executed :)

like image 30
Pawel Avatar answered Jan 02 '23 13:01

Pawel