पर वेरिएबल्स को पास कर रहा है। मैं JSON URL के सेट के माध्यम से लूप को JQuery $ .ajax का उपयोग कर रहा हूं और परिणामों को सरणी में डाउनलोड कर रहा हूं। कुछ यूआरएल 404 लौटाते हैं - जिसे मैं & को एक संदेश भेज रहा हूं।jQuery AJAX सफलता या त्रुटि फ़ंक्शन
हालांकि, मैं यूआरएल पास नहीं कर पा रहा हूं, अधिक सटीक यह हमेशा सरणी में अंतिम यूआरएल पास करता है।
मुझे लगता है क्योंकि ajax अतुल्यकालिक है और इसे पूरा करने के लिए समय लेता है कि है, लेकिन मुझे यकीन है कि और कैसे यकीन है कि केवल वर्तमान JSON url (या चर) बनाने के लिए सफलता या त्रुटि
मेरे कोड पर दिखाया जा रहा है नहीं कर रहा हूँ :
// For every URL loop through 'baseitems' array
for (var i = 0; i < baseitems.length; i++) {
// This is where I'm hoping to store the current URL as a variable to pass to the end-user on Success or Error
var caturl = baseURL + "/" + baseitems[i];
// Get the data via JSON
$.ajax({
type: "GET",
url: caturl,
dataType: "json",
async: true, // set so that the variables caturl get updated below
success: function (result) {
// Success: store the object into catalog array
cat.unshift(result);
$('#showdata').prepend("Loaded: " + caturl + "</br>"); // still buggy here - probably async JSON issue
},
error: function (xhr, textStatus, error) {
// Error: write out error
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
$('#showdata').prepend("ERROR : '" + error + "' trying to access: " + caturl + "</br>"); // still buggy here - probably async JSON issue
}
});
}
** अद्यतन: श्रमजीवी कोड **
@charlietfl मदद + कुछ अच्छी बात यह है के साथ पूरा काम कर कोड सफलता/त्रुटि कोड की तरह + लोड यूआरएल की गिनती नीचे है। धन्यवाद charlietfl & शांति निर्माता!
$.ajax({
type: "GET",
url: caturl,
dataType: "json",
async: true, // set so that the variables caturl get updated below
beforeSend: function (jqXHR, settings) {
/* add url property and get value from settings (or from caturl)*/
jqXHR.url = settings.url;
},
success: function (result, textStatus, jqXHR) {
// Success: store the object into catalog array
var url = jqXHR.url;
cat.unshift(result);
$('#showdata').prepend("<font size=\"1\">Loading: " + url + " status: " + textStatus + "</font></br>");
successcount += 1;
},
/* error to be deprecated in jQuery 1.8 , superseded by "fail" */
error: function (jqXHR, textStatus, error) {
var url = jqXHR.url;
/* replace caturl with url in your append */
$('#showdata').prepend("<font size=\"1\" color=\"red\">ERROR : '" + error + "' trying to access: " + url + "</font></br>");
},
complete: function (jqXHR, textStatus) {
$('#showdata').prepend("<font size=\"3\">Loaded <b>" + successcount + "</b> of " + baseitems.length + " total catalogs.</font></br>")
}
});
क्या आपने AJAX कॉल में caturl पर फ़ंक्शन में 'caturl' के मान को पारित करने का प्रयास किया है, फिर परिणाम में इसे वापस पास कर दिया है? वैकल्पिक रूप से, जैसा कि आप पहले से ही सर्वर पर यूआरएल जानते हैं, आप इसे AJAX सफलता 'परिणाम' पर वापस भेजते हैं और इसका उपयोग – peacemaker