/ / JQuery AJAX सफलता समारोह में एक जेएस पुष्टि संवाद लपेटना - jquery

JQuery AJAX सफलता समारोह में एक जेएस पुष्टि संवाद लपेटना - jquery

जेएस प्रदर्शित करने के लिए मेरे पास एक साधारण आवश्यकता हैडायनामिक वैल्यू पर निर्भर संवाद की पुष्टि करें। यह मान वेबपृष्ठ पर AJAX के माध्यम से संशोधित किया जा सकता है, इसलिए इसे पृष्ठ लोड पर हार्ड-कोड करना संभव नहीं है, इसलिए जब मैं ऑनक्लिक निकालता हूं तो इसे पकड़ने के लिए मैं jQuery.ajax () का उपयोग करता हूं। उदाहरण के लिए।

ध्यान दें कि AJAX कॉल सही मान देता है।

एचटीएमएल

    <a href="{% url program_pdf object.id %}" onClick="return check_time_budget();">
Download PDF
</a>

जे एस

    function check_time_budget() {
return jQuery.ajax({
url: "/program/check_duration/",
data: { "program_id": {{ instance.id }} },
success: function(response) {
console.log(response);
if (response.result != true){
return confirm("Your program estimated duration does not match the time allocated. Is this OK?");
}
else {
return true;
}
},
error: function() {
return false;
}
});
}

मैंने रिटर्न, सेट वर्र्स इत्यादि के साथ झुका दिया है, लेकिन ए टैग में सही मूल्य को सफलतापूर्वक वापस करने के लिए लिपटे सफलता समारोह को प्रतीत नहीं होता है।

क्या मैं प्राप्त करने का एक आसान तरीका है जिसके बाद मैं हूं "

संपादित करें: मैं Dajaxice और SetTimeout के साथ एक हैक का उपयोग करने के लिए वापस चला गया

एचटीएमएल

onClick="return dajaxice_wrapper(this, {{object.id}});"

जे एस

    var callback_status;
function dajaxice_check_time_budget(data) {
var msg = "Your program estimated duration does not match the time allocated.nnIs this OK?n";
if (data.result == true) {
callback_status = true;
}
else {
callback_status = confirm(msg);
}
}

function dajaxice_wrapper(obj, program_id) {
callback_status = false;
Dajaxice.programcreator.check_time_budget(dajaxice_check_time_budget, { "id": program_id });
// Required to get callback_status set from dajaxice callback (asynchronous call via ajax)
setTimeout(function() {
if ( callback_status != undefined ) {
if ( callback_status == true ) {
window.location = obj.href;
}
};
}, 500);
return callback_status;
}

उत्तर:

जवाब के लिए 0 № 1

एक दृष्टिकोण मैं सोच सकता हूं:

<a id="download-trigger" href="{% url program_pdf object.id %}" onClick="return check_time_budget();">Download PDF</a>

function check_time_budget() {
return jQuery.ajax({
url: "/program/check_duration/",
data: { "program_id": {{ instance.id }} },
success: function(response) {
console.log(response);
if (response.result != true){
return confirm("Your program estimated duration does not match the time allocated. Is this OK?");
}
else {
// This will start to load the pdf.
window.location = jQuery("#download-trigger").attr("href");
}
},
error: function() {
return false;
}
});
}