1

I am storing AJAX requests for further treatment in an array, with requests.push($.post('/validate', postData)); within a .each() loop.

When I dump these objects, the Chrome web inspector show this:

Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}

And so on, for each objects of the array. In these objects, I want to get the responseText (the data returned by the AJAX query). I can't figure out how to do it.

request.responseText doesn't seem to work.

ryancey
  • 1,037
  • 2
  • 11
  • 27
  • For jQuery you find that [**jQuery Docs**](http://api.jquery.com/) have a great set of demos and examples. Most of the basics are found in there immediately. The answer to your question is also there right at the top. [**MDN**](https://developer.mozilla.org/en-US/docs/Web/JavaScript) Is also a great resource. I'm not being sarcastic, I'm genuinely meaning this as well intended advise to save yourself some frustration in the long run. Off course you can always ask on SO but the docs usually give you faster results on the basics. Hope it helps. – Nope Oct 17 '13 at 11:16
  • I know the people discourage by giving -ve mark instead of guiding bigginers I hate that I am giving 1+ here – Sujith Wayanad Oct 17 '13 at 11:18
  • @FrançoisWahl my full code is a little more tricky. I store the requests because I have to be sure all the requests have been made and returned a response before executing another part of the code. I use this method : http://stackoverflow.com/a/16524738/2544016. I dump the elements in the `whenAll()` part. – ryancey Oct 17 '13 at 11:26
  • @ryancey: Ah think I see. I used `$.when(function1(), function2(), function3()).done(function () {//..all done})` myself when playing around with this [**here**](http://jsfiddle.net/FranWahl/VKrNN/). Each function returns a `$.Deferred`. Each function could execute a post and store the responses within each post's success callback. Then `done()` executes in which you could access the responses array and do what you need to do before you continue. You can even wrap that in another `when` and so on. I not sure if this helps but I hope it does somewhat. – Nope Oct 17 '13 at 11:34
  • @FrançoisWahl It worked by using `element.done(function(data) { console.log(data); });`. Thank you :) – ryancey Oct 17 '13 at 12:12
  • @ryancey: Nice, glad you got it to work. – Nope Oct 17 '13 at 12:40

2 Answers2

1

You're logging the ajax request, not the ajax response.

You'll need a success method, which will allow you to get to the response returned from the server.

Directly from the documentation:

$.post( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
});
royse41
  • 2,310
  • 4
  • 22
  • 29
0

Try it like,

$.post('/validate', postData,function(data){
    requests.push(data); // push response in array here
});
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106