Page 1 of 1

Laravel won't submit ajax

Posted: Tue Dec 31, 2013 9:56 am
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); }
  });

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 12:47 pm
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)?

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 12:51 pm
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.

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 1:24 pm
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.

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 1:33 pm
by makamo66
If I use "if ($this->request->ajax())" I get the error "Undefined property: UserController::$request"

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 2:07 pm
by requinix
requinix wrote:Replace "$this->request" with whatever it takes to get the request object.

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 3:24 pm
by Celauran
Request::ajax() is correct.
http://laravel.com/docs/facades

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 3:47 pm
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?

Re: Laravel won't submit ajax

Posted: Tue Dec 31, 2013 4:14 pm
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.