Laravel won't submit ajax

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Laravel won't submit ajax

Post by makamo66 »

I'm trying to get the user/dashboard page to submit as ajax. My console shows ABOUT TO SEND SUCCESS! Done. so that means the jquery post was sent but I keep getting "No ajax" on the user/dashboard page. How do I submit a page as ajax?

added to routes.php

Code: Select all

Route::post('user/dashboard', array('before' => 'suth', 'uses' => 'UserController@calendar'));
added to UsersController.php

Code: Select all

   public function calendar(){
	print_r($_POST);
 	if (Request::ajax()) {
        return "yeahhhh";
    	}
        return "<br />No ajax";
 		return View::make('user.dashboard.index');
}
added to main.js

Code: Select all

$.ajax({
    url: 'http://beta.opentemp.local/user/dashboard',
    data: {} + "&_token=" + $("input[name=_token]").val(),
    type: 'POST',
    'beforeSend': function(xhr, settings) {
      console.log('ABOUT TO SEND');
    },
    'success': function(result, status_code, xhr) {
      console.log('SUCCESS!');
    },
    'complete': function(xhr, text_status) {
      console.log('Done.');
    },
    'error': function (XMLHttpRequest, textStatus, errorThrown) {

	alert("Error with ajax Function: "+ textStatus+" "+errorThrown); }
  });
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel won't submit ajax

Post by requinix »

Code: Select all

data: {} + "&_token=" + $("input[name=_token]").val(),
That's no good. Putting aside how that's not really a valid thing to do, it's not what you're supposed to do. The data is either an object

Code: Select all

data: { _token: $("input[name=_token]").val() },
or a string

Code: Select all

data: "_token=" + $("input[name=_token]").val(),
The actual problem is that your PHP code doesn't think the request is from AJAX. Assuming one of the above changes doesn't fix it, what's the code for Request::ajax() (so we can see what all it is doing to decide if a request is via AJAX or not)?
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Re: Laravel won't submit ajax

Post by makamo66 »

added to UsersController.php

Code: Select all

   public function calendar(){
	print_r($_POST);
 	if (Request::ajax()) {
        return "yeahhhh";
    	}
        return "<br />No ajax";
 		return View::make('user.dashboard.index');
}
That's all it's doing to decide if it's ajax.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel won't submit ajax

Post by requinix »

So nevermind my question, I'm looking at the source instead. Easier.

Request::ajax() is not a static method. It is an instance method. You have to call it on an instance of the class. I don't know Laravel but you probably need something like

Code: Select all

if ($this->request->ajax()) {
Replace "$this->request" with whatever it takes to get the request object.
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Re: Laravel won't submit ajax

Post by makamo66 »

If I use "if ($this->request->ajax())" I get the error "Undefined property: UserController::$request"
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel won't submit ajax

Post by requinix »

requinix wrote:Replace "$this->request" with whatever it takes to get the request object.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Laravel won't submit ajax

Post by Celauran »

Request::ajax() is correct.
http://laravel.com/docs/facades
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Laravel won't submit ajax

Post by Celauran »

I'm not sure we have enough information. The AJAX request itself is clearly working fine. Instead of console.log('success') have you tried logging the response? You should see the output of your print_r($_POST) in there. Is there any additional logic surrounding the $.ajax() call? Are you viewing this FROM /user/dashboard?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Laravel won't submit ajax

Post by requinix »

Celauran wrote:Request::ajax() is correct.
http://laravel.com/docs/facades
I'm probably looking at the wrong code then?
laravel/framework, src/Illuminate/Http/Request.php

Code: Select all

        /**
         * Determine if the request is the result of an AJAX call.
         *
         * @return bool
         */
        public function ajax()
        {
                return $this->isXmlHttpRequest();
        }
[edit] Oh, I see. Well that's certainly annoying.
Post Reply