2016-09-02 3 views
2

Ich habe verschiedene Ajax-Aufrufe zu gleichen API mit anderen Pfad und Abfrage in URL, aber mit mit gleichen jsonpCallback kann ich nichtmehrere Ajax mit dem gleichen jsonpCallback wie Rückruf getan

ändern, wenn ich eine Ajax genannt URL - alles in Ordnung!

Aber wenn ich Antworten von mehreren erhalten muss, habe ich Fehler, verursachen ihre Ausführung sofort. Ich habe versucht Rückrufe, tryed I "async: false", tryed I „Rückkehr Ajax mit wenn, dann, etc gemacht - nichts funktioniert, wie ich :(erwartet

function timenow() { 
    var a = new Date, 
    b = a.getMinutes(); 
    b < 10 && (b = "0" + b); 
    var c = a.getHours(); 
    c > 12 && (c -= 12); 
    var d = c + ":" + b; 
    return d 
} 

function ajaxCall(p, callback) { 
    if (p == 1) { 
    url = 'https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults' 
    } else { 
    url = "https://api.github.com/users/jeresig?callback=logResults" 
    } 
    return $.ajax({ 
    url: url, 
    dataType: "jsonp", 
    jsonp: false, 
    jsonpCallback: "logResults", 
    cache: true, 
    }).done(function(r) { 
    callback(r); 
    }); 
} 

$.when(
    ajaxCall(0, function(r) { 
    console.log(r); 
    console.log(r.data.avatar_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r); 
    console.log(r.response[1].id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.bio + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.blog + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].from_id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.company + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].to_id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.created_at + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].date + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.email + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].post_type + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.events_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].text + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.followers + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].is_pinned + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.followers_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].reposts.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.following + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].likes.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.following_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].comments.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.gists_url + ' ' + timenow()); 
    }) 
).done(console.log('Ajaxing done ' + timenow())); 

LOG ERROR

Ich denke Problem in gleichen jsonpCallback , so dass, wenn ich Rückruf in ajax verwende es immer umschreibt es mit neuer Funktion, so dass ich bin immer Fehler.

PS kann immer noch nicht eingerichtet async auf false Probe arbeitet https://jsfiddle.net/ypmkn5k0/

<div id="log"></div> 

<script> 
$.ajaxSetup({ async: false}); 


$.ajax({ 
    url: 'https://api.github.com/users/jeresig?callback=logResults', 
    dataType: "jsonp", 
    jsonp: false, 
    jsonpCallback: "logResults", 
    cache: true, 
    async: false, 
}).done(function(r) { 
    $('#log').append(r.data.avatar_url+'</br>'); 
}); 

$('#log').append('Ajaxing done </br>'); 
</script> 
Oh

P.S.S, überprüfte ich Dokumentation noch einmal, jetzt verstehe ich, sehr traurig (((

domänenübergreifende Anfragen und Datentyp: „JSONP“ Anfragen nicht unterstützen synchronen Betrieb.

PSSS Ich habe mit callbacking letzte versuchen und es ist WORKS, FUK-FUK-FUK, weiß nicht, warum es nicht auf realen Projekt, aber ich werde es EMPLIMENT TRY:

function timenow(){var a=new Date,b=a.getMinutes();b<10&&(b="0"+b);var c=a.getHours();c>12&&(c-=12);var d=c+":"+b;return d} 

     function ajaxCall(p,callback) { 
     if(p==1){ 
     url='https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults' 
     } else { 
     url="https://api.github.com/users/jeresig?callback=logResults" 
     } 
     $.ajax({ 
     url: url, 
     dataType: "jsonp", 
     jsonp: false, 
     jsonpCallback: "logResults", 
     cache: true, 
     success: function (r) { 
     callback(r); 
     }, 
     }); 
     } 


     ajaxCall(0,function(r){console.log(r); console.log(r.data.avatar_url+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r); console.log(r.response[1].id+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.bio+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].id+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.blog+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].from_id+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.company+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].to_id+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.created_at+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].date+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.email+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].post_type+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.events_url+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].text+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.followers+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].is_pinned+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.followers_url+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].reposts.count+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.following+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].likes.count+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.following_url+' '+timenow()); 
     ajaxCall(1,function(r){console.log(r.response[1].comments.count+' '+timenow()); 
     ajaxCall(0,function(r){console.log(r.data.gists_url+' '+timenow()); 
      console.log('Ajaxing done '+timenow()); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     }); 
     });    
     }); 
     }); 
     }); 

enter image description here

+0

Sie können nicht gleichzeitig ausgeführt werden, weil Javascript single-threaded ist. – Barmar

+0

Wo ist die Definition von 'logResults'? – Barmar

+0

Können Sie bitte die Fehlerinformationen angeben, die Sie erhalten? Der Hauptpunkt von AJAX ist die Asynchronität, und Sie können den obigen Kommentar ignorieren, denn sobald eine Ajax-Kommunikation gestartet wird, wird der Rest der Funktion fortgesetzt. –

Antwort

1

Das Hauptproblem, das ich sehe, ist in Ihrer .done() am Ende. Dies sollte in einer Funktion enthalten sein. Das und ich denke, es wäre besser, wenn Sie Ihre 'URL' Variable deklarieren, so dass es keine Verwirrung über seine Lokalisierung gibt.

function timenow() { 
    var a = new Date, 
    b = a.getMinutes(); 
    b < 10 && (b = "0" + b); 
    var c = a.getHours(); 
    c > 12 && (c -= 12); 
    var d = c + ":" + b; 
    return d 
} 

function ajaxCall(p, callback) { 
    var url = 'https://api.github.com/users/jeresig?callback=logResults'; 
    if (p == 1) { 
    url = 'https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults' 
    } 
    return $.ajax({ 
    url: url, 
    dataType: "jsonp", 
    jsonp: false, 
    jsonpCallback: "logResults", 
    cache: true, 
    }).done(function(r) { 
    callback(r); 
    }); 
} 

$.when(
    ajaxCall(0, function(r) { 
    console.log(r); 
    console.log(r.data.avatar_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r); 
    console.log(r.response[1].id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.bio + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.blog + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].from_id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.company + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].to_id + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.created_at + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].date + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.email + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].post_type + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.events_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].text + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.followers + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].is_pinned + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.followers_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].reposts.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.following + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].likes.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.following_url + ' ' + timenow()); 
    }), 
    ajaxCall(1, function(r) { 
    console.log(r.response[1].comments.count + ' ' + timenow()); 
    }), 
    ajaxCall(0, function(r) { 
    console.log(r.data.gists_url + ' ' + timenow()); 
    }) 
).done(function() { console.log('Ajaxing done ' + timenow()); }); 

Wenn Sie die async verwenden werden: falsches Argument, dann gibt es keinen Punkt in all diese in einem .when/.done umschließt, und wenn Sie wissen, dass Sie nicht diese asynchron aufrufen können, dann Ich würde empfehlen, dass Sie die .when-Logik entfernen und einfach die Funktion sequenziell mit der Eigenschaft async: false aufrufen.

Es wäre auch hilfreich, Ihren jsonpCallback zu sehen. Wenn Sie Variablen oder Ressourcen haben, die als gemeinsam genutzt werden, könnte das auch ein Problem sein.

+0

Sie hatten Recht über func in .done, aber wie man es async zu falsch einrichtet? asynchronicity https://jsfiddle.net/ypmkn5k0/ :( –

+1

Sie können einen Ansatz ähnlich Rekursion verwenden, wenn Async Ihnen Probleme gibt. Schauen Sie sich das an und lassen Sie mich wissen, wenn Sie Fragen haben. https: // jsfiddle .net/ypmkn5k0/2/ –

+1

Ich weiß nicht, dein Ende Spiel, aber hier ist etwas in Bezug auf eine sauberere, leichter zu lesen Ansatz zu berücksichtigen. https://jsfiddle.net/ypmkn5k0/3/ –

Verwandte Themen