javascript - Passing variables to an AJAX function through the parameters PHP

Solution:
Re-write thesetTimeout()
call like so:
setTimeout(function() {
myAjax(url,action,id,timeout);
}, timeout);
Answer
Solution:
The second time it is called (from yoursuccess
handler), you are not passing any parameters tomyAjax()
so it will not have any arguments when it is invoked the second time.
There are a couple ways you can fix that. One way it to just copy the arguments when you callmyAjax(...)
from within:
function myAjax(url,action,id,timeout) {
$.ajax({
type: "POST",
url: url,
data:{action: action},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( id ).innerHTML = data;
setTimeout(function() {
myAjax(url, action, id, timeout);
}, timeout);
}
});
}
But, you could also make an inner function and then just reference the arguments from the closure like this:
function myAjax(url,action,id,timeout) {
function run() {
$.ajax({
type: "POST",
url: url,
data:{action: action},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( id ).innerHTML = data;
setTimeout(run, timeout);
}
});
}
// run the ajax function the first time
run();
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php_network_getaddresses: getaddrinfo failed: temporary failure in name resolution
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.