Parijat Kalia
Parijat Kalia

Reputation: 5085

jQuery post, access responseType or response in callback

I am trying to implement a jQuery ajax post that reads like this :

$(this).click( function() { 
    jQuery.post('index', function() {
           console.log(responseText); 
    }, "text") 
});

In the above. The ajax call is definitely taking place, so I am non-plussed about that. What I am trying to wrap my head around is how I can access the responseText in my callback function, which is basically the Ajax response that is being received upon successful execution of the ajax call.

Pretty simple I suppose, but I can't get it :p

Upvotes: 0

Views: 229

Answers (3)

insomiac
insomiac

Reputation: 5664

Callback function returns responseText which you need to add:

$(this).click( function() { 
   jQuery.post('index', function(responseText) {
       console.log(responseText); 
   }, "text") 
});

jquery post documentation : http://api.jquery.com/jQuery.post/

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245429

The response is passed as an argument to your success callback function. All you need to do is add the parameter to your function and then access it:

jQuery.post('index', function(response){
    console.log(response);
});

If you need more data, you'll have to go with something that is a little bit more verbose. You'd be best following the answer to this question:

jquery how to check response type for ajax callback - Stack Overflow

Upvotes: 2

Ry-
Ry-

Reputation: 224962

It's passed as an argument to the callback. Reading the documentation can be helpful :)

$(this).click(function() {
    jQuery.post('index', function(responseText) {
        console.log(responseText);
    }, "text")
});

Upvotes: 3

Related Questions