I have very stange problem: I have product list and when you click on product to edit the product you go to product form. On this product form I have AJAX that is sending request to getFiltersGroup
. I want to resend this AJAX on error, but for some reason on error AJAX is start sending url request for product form not for getFiltersGroup
.
Any ideas what is wrong, why is not sending request to getFiltersGroup
?
This is what happen on retry:
This is the code:
var retry;
$('#filters-ajax').on('click', function(){
var product_id = "<?php echo isset($set_id) ? $set_id : NULL; ?>";
var ajaxConfig = $.ajax({
url: "index.php?route=catalog/product/getFiltersGroup&token=<?php echo $token ?>",
type: "POST",
data: '&product_id='+product_id,
dataType: 'json',
beforeSend: function(){
$('#btn-product-filter-groups').prop('disabled', true);
},
success: function(data){
clearInterval(retry);
$('#btn-product-filter-groups').prop('disabled', false);
},
error: function(xmlhttprequest, textstatus, message) {
retry = setInterval(function(){
$.ajax(ajaxConfig);
}, 6000)
}
});
})
UPDATE
I fix the url request but now I can’t reset the setInverval()
. Also I’m never getting alert message. Any ideas what wrong?
var retry;
$('#filters-ajax').on('click', function(){
var product_id = "<?php echo isset($set_id) ? $set_id : NULL; ?>";
$.ajaxSetup({
url: "index.php?route=catalog/product/getFiltersGroup&token=<?php echo $token ?>",
type: "POST",
data: '&product_id='+product_id,
dataType: 'json'
});
var ajaxConfig = $.ajax({
beforeSend: function(){
$('#btn-product-filter-groups').prop('disabled', true);
},
success: function(data){
alert('WTF')
clearInterval(retry);
$('#btn-product-filter-groups').prop('disabled', false);
},
error: function(xmlhttprequest, textstatus, message) {
retry = setInterval(function(){
$.ajax(ajaxConfig);
}, 6000)
}
});
})
2
Answers
I dont know why but this one is working if anyone can explain why this code is working and previous code is not working i will accept his answer
$.ajax()
returns a jqXHR object, which you are later passing to$.ajax()
as a configuration object for a new call:But that jqXHR object is not an
$.ajax()
config object, and it will not have aurl: ...
key/value pair (nor any of your other settings). Checking the docs for the$.ajax()
config object it says (emphasis mine):And regarding the
url
setting:So since there is no URL specified in the config object, it will default to the current page. Your new
$.ajax()
request will request the current page, your edit page.If you want to make your AJAX call a callable function, just put it in a function and call it:
This approach sends off all kinds of recursive alarm bells for me though, I would consider trying another approach.