PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri May 29, 2020 8:20 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Dec 31, 2013 10:56 am 
Offline
Forum Newbie

Joined: Wed May 12, 2010 6:13 pm
Posts: 24
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
Syntax: [ Download ] [ Hide ]
Route::post('user/dashboard', array('before' => 'suth', 'uses' => 'UserController@calendar'));


added to UsersController.php
Syntax: [ Download ] [ Hide ]
   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

Syntax: [ Download ] [ Hide ]
$.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); }
  });
 


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 1:47 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Syntax: [ Download ] [ Hide ]
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
Syntax: [ Download ] [ Hide ]
data: { _token: $("input[name=_token]").val() },

or a string
Syntax: [ Download ] [ Hide ]
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)?


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 1:51 pm 
Offline
Forum Newbie

Joined: Wed May 12, 2010 6:13 pm
Posts: 24
added to UsersController.php
Syntax: [ Download ] [ Hide ]
   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.


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 2:24 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
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
Syntax: [ Download ] [ Hide ]
if ($this->request->ajax()) {

Replace "$this->request" with whatever it takes to get the request object.


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 2:33 pm 
Offline
Forum Newbie

Joined: Wed May 12, 2010 6:13 pm
Posts: 24
If I use "if ($this->request->ajax())" I get the error "Undefined property: UserController::$request"


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 3:07 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 4:24 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Request::ajax() is correct.
http://laravel.com/docs/facades

_________________


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 4:47 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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?

_________________


Top
 Profile  
 
PostPosted: Tue Dec 31, 2013 5:14 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group