After page fully loaded, i make ajax request to an action.
While waiting for response from action (it takes 2 or 3 seconds), if user clicks on other link, i want to abort previous request and kill mysql process at once.
How can i do this?
I tried to do like this
var xhr = $.ajax({
/*
params
*/
});
//before unload page
xhr.abort();
but i think, that it will not kill sql process.
Make a named ajax. Here is ajx is named
var formData = {};//Your data for post
var ajx = $.ajax({
type: "POST",// Http verbs
url: "filename.php",// file to request
data: formData,
success: function(response){
console.log("ajax completed");
console.log(response);
}
});
Stop ajax request by clicking button
$( "#button_id" ).click(function() {
ajx.abort();
});
Related
I have written a small script to make an Ajax request. I use jQuery for this purpose. I have done this several times but this is the first time I get a failure message. This is my script:
$(document).ready(function(){
$("#btnExecute").click(function(){
var numberOfClusters = $('#inputNumCab').val();
alert(numberOfClusters);
$.ajax({
type: "POST",
url: 'RunPythonThroughPHP/insertData_2.php',
//data: { numberOfClusters: numberOfClusters },
success: function(msg){
alert('thanks');
},
error: function(){
alert("failure");
}
});
});
});
The concept is the following. When I press the button (which is in a form) I get the value of a text field (based on id) and I pass this data into my ajax request. The problem is I get the failure alert.
Why this happens? If I make my request not async (async: false) then it works.
I am using Laravel as a PHP framework and I have Jquery set up so it prompts the user if they want to confirm redirecting away from the page or not.
The problem is that if the user confirms the redirect away from the page, I need to somehow perform some database operations to set various statues to closed on my tables.
How is this possible?
My Jquery code prompting the user
<script type="text/javascript">
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
Just make an Ajax request to inform the server that the user will be redirectd.
$(window).bind('beforeunload', function(e){
if ( confirm("Are you sure you want to leave?") ) {
e.preventDefault();
// make ajax request to inform the server
$.ajax({
type: "POST",
url: reqUrl,
data: reqBody,
dataType: "json",
success: function(data, textStatus) {
// handle things
}
});
// Redirect.
window.location.href = someURL;
}
});
I am developing a custom emailing system for one of my clients as an add on for the CMS I built that I give to my clients. I am using jQuery Ajax to send all the information to a PHP file that formats the email, sends a query to MySQL to get all the email address from the database, and the sends the emails using the mail(). This takes some time to do, and I am wanting the jQuery ajax to display a progress bar for each time the PHP script sends an email. I have searched for something that is similar to success: function() that receives data from the PHP script through JSON allowing jquery to update the progress of the emailing.
Does anyone have a suggestion for this? Something like this is preferable:
$.ajax({
type: "POST",
url: "example.com",
data: {"test":"test","tester":"tester"},
PROGRESS: function(data){
$("div").html(data);
},
success: function(r){
alert(r);
}
});
});
You can use the xhr object and attach an event listener to the progress event
$.ajax({
//...
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.addEventListener("progress", function(e) {
var p = e.loaded / e.total;
// update your progress bar here..
});
return xhr;
}
})
You will need to wrap this inside of a setInterval() function. You can search for AJAX Polling. But the idea here is that you cannot send information back periodically from the same PHP script while it is running, the success callback is only triggered ONCE, after the script has finished.
I have some ajax script that fire off about 250 synchronous PHP calls . This is my script
$(document).ready(function(){
$("#generate").html("<div class='modal'><p>Initializing...</p></div>");
$.ajax({
url:'/fetch around 250 url from database.php',
async:false,
dataType: 'json',
success: function(data){
$.each(data,function(key,val){
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
saveimage(val.url);
}
$("#generate").html("<div class='modal'><p>done</p></div>");
finalcreate();
},
});
});
function saveimage(){
$.ajax({
url: 'do some php work.php',
async: false,
});
}
function finalcreate(){
$.ajax({
url: 'do some php work.php',
async: false,
});
}
In the first part script fetch more than 250 urls from database and for every url script do some php calculation using another ajax call. when the loop ends script do final ajax call.
When i run this programe in firefox, it run successfully for only 40 urls, then browser shows dialog box with option of whether user want to stop this script or not, if user want to run this script then the script run again for next 40 urls , same proccess occure till the end.
How i can optimize this script, i dont want browser show option to stop this script. Please help.
Thanks
Try this:
function nextrequest() {
if (requests.length == 0) {
$("#generate").html("<div class='modal'><p>done</p></div>");
finalcreate();
return;
}
var val = requests.pop();
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
saveimage(val.url);
}
var requests = [];
$(document).ready(function(){
$("#generate").html("<div class='modal'><p>Initializing...</p></div>");
$.ajax({
url:'/fetch around 250 url from database.php',
dataType: 'json',
success: function(data){
requests = data;
nextrequest();
},
});
});
function saveimage(){
$.ajax({
url: 'do some php work.php',
success: function(data) {
// do something...
nextrequest();
}
});
}
function finalcreate(){
$.ajax({
url: 'do some php work.php',
});
}
You store all the URLs in a global variable, and everytime a request is done, you get the next one, until all of them are consumed, (requests.length == 0), you call the final request.
This way the user can still do something else on the page, and you can display progress everytime a request is done. Also, a good thing is that you can make 2 calls at once, or more, to make the process faster.
Ajax call needs much time to complete, as it communicates with remote server. The slowest thing there is a query to the server. You should send one batch request with all data needed to the server, that should separate the data and handle it. Everything should be completed about 250 times faster.
make some time interval for each ajax request
success: function(data){
$.each(data,function(key,val){
$("#generate").html("<div class='modal'><p>Fetching "+val.url+"</p></div>");
setTimeout(saveimage(val.url),3000);
}
I've got a stack of ajax request:
$("someTable tr").each(function() {
// send the data.
var scriptURL = "/module/action/data/" + $(this).find(".data").html() + "/";
document.cyonVars.xhrPool[count] = $.ajax({
type: "GET",
url: scriptURL,
queue: "autocomplete",
cancelExisting: true,
dataType: 'json',
success: function(data){
// do something
}
}
});
count++;
})
While these requests are running the user can press a button. This triggers another ajax request. Something like that:
var scriptURL = "/module/anotheraction/" +
data) + "/";
$.ajax({
type: "GET",
url: scriptURL,
queue: "autocomplete",
cancelExisting: true,
dataType: 'json',
success: function(data){
// Do another thing
}
});
The requests from the first action responding asynchrone as I wish. When a user triggers the second request that one waits till the other requests are finished. But the second request should be proceed earlier. I already worked with session_write_close() didn't changed anything. Thanks for helping.
i'm not so sure, that it should be processed futher. browsers have limits on connections to the same server and if it takes long for a request to reply your app might stop on few or more requests. just think, that async requests are shoot asynchronously (code after ajax request continues to execute), but requests are executed in sequence.
The problem was Zend_Session::Start();
I had to avoid using Zend_Session while proceeding async requests