I’m trying to learn to use jquery deferreds.
When using html on jsfiddle, I get an object returned that when printed in the then() statement, has two lines: “success” and the html that is returned by the ajax request.
So that when I do this:
$(document).on('click', '.ajax', function() {
$.when(ajax1('<p>first</p>'),
ajax2('<p>second</p>'),
ajax3('<p>third</p>'))
.then(function(results1, results2, results3) {
console.log(results1);
$('.document').append(results1);
$('.document').append(results2);
$('.document').append(results3);
alert('all ajax done');
});
});
http://jsfiddle.net/loren_hibbard/AsgDz/
I get this in the console:
["<p>first</p>", "success",
Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
overrideMimeType: function ( type ) {
pipe: function ( /* fnDone, fnFail, fnProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseText: "<p>first</p>"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( /* fnDone, fnFail, fnProgress */ ) {
__proto__: Object
How can I access just that responseText and avoid the html output with the “success” every other line:
first
success
second
success
third
success
And since I will be more likely using it with JSON data, how do I parse that. Right now, though it returns ‘ajax all complete’ in my then() function, the returned object in the console is a set of three objects. One is the parsed json string, the other is the annoying strting that says ‘success’, and the third is the large object, which contains the responseText which is my unparsed JSON object. How can I access this first object and print my parsed string?
http://jsfiddle.net/loren_hibbard/jtvHf/1/
Thank you!
Simple fix — each result object is an array with three items:
So just use
results1[0], etc to get the data:DEMO
(Note, it looks like there was also a typo in your
ajax3function — the “type” was set tohtmlinstead of `post’.)