即使在页面重定向后,ajax 调用也会完成吗?
Will an ajax call finish even after a page redirect?
我正在尝试为本地图片创建临时图片 url 并将其发送到 Google 以按图片搜索。我不希望图像 url 永久存在,所以我想在使用后立即将其删除。我有以下代码:
// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
var xml = new XMLHttpRequest();
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
deleteImageURL(); // asynchronous call to server to delete the URL
window.location.href =
"https://www.google.com/searchbyimage?site=search&sa=X&image_url="
+ xml.responseText; // the image url
}
}
xml.open("GET", "REST_ENDPOINT", true);
xml.send();
}
上面的函数调用服务器,当它完成时,将删除 url 并重定向页面。函数 "deleteImageURL()" 是另一个 ajax 异步完成的调用。目前,这会正常加载 google 页面,因为在重定向发生时图像 URL 尚未删除 url。
我的问题是:即使在页面重定向之后,deleteImageURL() 是否会完成删除图像 URL 或它会停止(因此永远不会删除 URL) ?
编辑:所以我在考虑你们所说的关于竞争条件的内容并尝试了以下代码:
// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
var xml = new XMLHttpRequest();
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
deleteImageURL(xml.responseText);
}
}
xml.open("GET", "REST_ENDPOINT"
+ id + "/public_url", true);
xml.send();
}
// Deletes the url for the image.
function deleteImageURL(imageURL) {
var xml = new XMLHttpRequest();
xml.open("GET", "REST_ENDPOINT_FOR_DELETE", true);
xml.send();
window.location.href =
"https://www.google.com/searchbyimage?site=search&sa=X&image_url="
+ imageURL;
}
每次我 运行 这段代码都有效。我认为仍然可能存在竞争条件,但到目前为止它似乎工作正常。
再次感谢。
如果 deleteImageURL();
包含异步调用,您应该在调用完成后进行重定向。当调用同步时,您的代码将起作用。我们看不到 deleteImageURL();
的来源,可以更具体一些,但您应该做与 getImageURL()
.
相同的事情
即使在页面重定向后,"deleteImageURL()" 也会完成删除图像 URL。
参考:Should I wait for ajax to complete to redirect a page?
服务器不会停止处理请求(由 deleteImageUrl 发起),但如果当前页面在浏览器中卸载之前您将无法处理回调操作完成。
我正在尝试为本地图片创建临时图片 url 并将其发送到 Google 以按图片搜索。我不希望图像 url 永久存在,所以我想在使用后立即将其删除。我有以下代码:
// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
var xml = new XMLHttpRequest();
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
deleteImageURL(); // asynchronous call to server to delete the URL
window.location.href =
"https://www.google.com/searchbyimage?site=search&sa=X&image_url="
+ xml.responseText; // the image url
}
}
xml.open("GET", "REST_ENDPOINT", true);
xml.send();
}
上面的函数调用服务器,当它完成时,将删除 url 并重定向页面。函数 "deleteImageURL()" 是另一个 ajax 异步完成的调用。目前,这会正常加载 google 页面,因为在重定向发生时图像 URL 尚未删除 url。
我的问题是:即使在页面重定向之后,deleteImageURL() 是否会完成删除图像 URL 或它会停止(因此永远不会删除 URL) ?
编辑:所以我在考虑你们所说的关于竞争条件的内容并尝试了以下代码:
// Gets a URL that can be used to do a search by image through Google.
function getImageURL() {
var xml = new XMLHttpRequest();
xml.onreadystatechange = function () {
if (xml.readyState == 4 && xml.status == 200) {
deleteImageURL(xml.responseText);
}
}
xml.open("GET", "REST_ENDPOINT"
+ id + "/public_url", true);
xml.send();
}
// Deletes the url for the image.
function deleteImageURL(imageURL) {
var xml = new XMLHttpRequest();
xml.open("GET", "REST_ENDPOINT_FOR_DELETE", true);
xml.send();
window.location.href =
"https://www.google.com/searchbyimage?site=search&sa=X&image_url="
+ imageURL;
}
每次我 运行 这段代码都有效。我认为仍然可能存在竞争条件,但到目前为止它似乎工作正常。
再次感谢。
如果 deleteImageURL();
包含异步调用,您应该在调用完成后进行重定向。当调用同步时,您的代码将起作用。我们看不到 deleteImageURL();
的来源,可以更具体一些,但您应该做与 getImageURL()
.
即使在页面重定向后,"deleteImageURL()" 也会完成删除图像 URL。 参考:Should I wait for ajax to complete to redirect a page?
服务器不会停止处理请求(由 deleteImageUrl 发起),但如果当前页面在浏览器中卸载之前您将无法处理回调操作完成。