Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.
Moderator: General Moderators
makamo66
Forum Newbie
Posts: 24 Joined: Wed May 12, 2010 6:13 pm
Post
by makamo66 » Tue Dec 31, 2013 9:56 am
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); }
});
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Dec 31, 2013 12:47 pm
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
Post
by makamo66 » Tue Dec 31, 2013 12:51 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Dec 31, 2013 1:24 pm
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
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
Post
by makamo66 » Tue Dec 31, 2013 1:33 pm
If I use "if ($this->request->ajax())" I get the error "Undefined property: UserController::$request"
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Dec 31, 2013 2:07 pm
requinix wrote: Replace "$this->request" with whatever it takes to get the request object.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Dec 31, 2013 3:24 pm
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Tue Dec 31, 2013 3:47 pm
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?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Tue Dec 31, 2013 4:14 pm
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.