0

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

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77

3 Answers3

3

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")
});
Ry-
  • 218,210
  • 55
  • 464
  • 476
2

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

Community
  • 1
  • 1
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1

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/

insomiac
  • 5,648
  • 8
  • 45
  • 73