Javascript 如何在正在运行的ajax调用之间打开URL/链接并销毁所有正在运行的ajax调用?
我正在处理一个对服务器进行ajax调用的网页,从服务器(控制器)再次调用一个WCF服务(这需要时间)来获取一些数据 在服务器(控制器)内部,我使用Task和AsyncWait并行调用服务 我的问题是: 打开包含用于调用控制器和WCF服务的代码的页面后,我无法通过单击UI中的锚定选项卡将我的选项卡/页面重定向到另一个URL。直到检索到ajax调用结果 用户界面代码:Javascript 如何在正在运行的ajax调用之间打开URL/链接并销毁所有正在运行的ajax调用?,javascript,ajax,web-services,async-await,task-parallel-library,Javascript,Ajax,Web Services,Async Await,Task Parallel Library,我正在处理一个对服务器进行ajax调用的网页,从服务器(控制器)再次调用一个WCF服务(这需要时间)来获取一些数据 在服务器(控制器)内部,我使用Task和AsyncWait并行调用服务 我的问题是: 打开包含用于调用控制器和WCF服务的代码的页面后,我无法通过单击UI中的锚定选项卡将我的选项卡/页面重定向到另一个URL。直到检索到ajax调用结果 用户界面代码: $(function () { if (AutomationType.toLowerCase() === "d
$(function () {
if (AutomationType.toLowerCase() === "desktop") {
$.ajax({
async: true,
url: "/" + AutomationType + "/Home/GetAllController",
data: { "hostName": hostname},
type: 'POST',
dataType: 'json'
}).success(function (response) {
debugger;
})
}
});
我已经尝试中止ajax调用,如下所示
$(function() {
$.xhrPool = [];
$.xhrPool.abortAll = function() {
$(this).each(function(i, jqXHR) { // cycle through list of recorded connection
jqXHR.abort(); // aborts connection
$.xhrPool.splice(i, 1); // removes from list by index
});
}
$.ajaxSetup({
beforeSend: function(jqXHR) { $.xhrPool.push(jqXHR); }, // annd connection to list
complete: function(jqXHR) {
var i = $.xhrPool.indexOf(jqXHR); // get index for current connection completed
if (i > -1) $.xhrPool.splice(i, 1); // removes from list by index
}
});
})
// Everything below this is only for the jsFiddle demo
$('a').click(function () {
$.xhrPool.abortAll();
});
服务器(控制器)代码
public async Task<JsonResult> GetAllController(string hostName)
{
string IsControllerRunning = string.Empty;
var currentHost = string.Empty;
var currentRunId = string.Empty;
var currentStatus = string.Empty;
var ipDns = string.Empty;
Stopwatch sw = new Stopwatch(); sw.Start();
List<List<ExecutionStepResult>> returnresultArray = new List<List<ExecutionStepResult>>();
List<Task<IEnumerable<ExecutionStepResult>>> taskList = new List<Task<IEnumerable<ExecutionStepResult>>>();
Debug.WriteLine("starting 1 " + sw.Elapsed);
var resultArray = hostName.TrimEnd('^').Split('^');
for (int i = 0; i < resultArray.Length; i++)
{
string host = resultArray[i];
Task<IEnumerable<ExecutionStepResult>> task = new Task<IEnumerable<ExecutionStepResult>>(() => getServiceResultByTask(host));
task.Start();
taskList.Add(task);
}
foreach (Task<IEnumerable<ExecutionStepResult>> taskitem in taskList)
{
try
{
Debug.WriteLine("calling task " + sw.Elapsed);
IEnumerable<ExecutionStepResult> val = await taskitem;
returnresultArray.Add(val.ToList());
}
catch (Exception ex)
{
returnresultArray.Add(new List<ExecutionStepResult>() { new ExecutionStepResult() { IsError = true, ErrorMessage="true" ,CustomMessage = ex.Message.ToString() } });
}
}
for (int i = 0; i < resultArray.Length; i++)
{
string host = resultArray[i];
currentHost = host.Split('|').GetValue(1).ToString();
currentStatus = host.Split('|').GetValue(2).ToString();
currentRunId = host.Split('|').GetValue(0).ToString();
ipDns = host.Split('|').GetValue(3).ToString();
List<ExecutionStepResult> exeResponse = returnresultArray[i].ToList();
if (exeResponse.Count() > 0 && (currentStatus != "3" || (currentStatus == "3" && exeResponse[i].ErrorMessage == "true")))
IsControllerRunning += host + "|" + exeResponse[0].CustomMessage + "^";
else if (exeResponse.Count() > 0 && currentStatus == "3" && exeResponse[0].ErrorMessage == "false")
IsControllerRunning += host;
}
Debug.WriteLine("end " + sw.Elapsed);
sw.Stop();
return Json(IsControllerRunning, JsonRequestBehavior.AllowGet);
}
公共异步任务GetAllController(字符串主机名)
{
string IsControllerRunning=string.Empty;
var currentHost=string.Empty;
var currentRunId=string.Empty;
var currentStatus=string.Empty;
var ipDns=string.Empty;
秒表sw=新秒表();sw.Start();
List returnresultArray=新列表();
List taskList=新列表();
Debug.WriteLine(“启动1”+软件运行时间);
var resultArray=hostName.TrimEnd('^').Split('^');
for(int i=0;i $(function () {
if (AutomationType.toLowerCase() === "desktop") {
$.ajax({
async: true,
url: "/" + AutomationType + "/Home/GetAllController",
data: { "hostName": hostname},
type: 'POST',
dataType: 'json'
}).success(function (response) {
debugger;
})
}
});
私有IEnumerable getServiceResultByTask(字符串主机)
{
使用(var service=new RemoteCommandClient())
{
尝试
{
系统线程线程睡眠(15000);
字符串currentHost=hosts.Split(“|”).GetValue(1.ToString();
字符串currentStatus=hosts.Split(“|”).GetValue(2.ToString();
字符串currentRunId=hosts.Split(“|”).GetValue(0.ToString();
字符串ipDns=hosts.Split(“|”).GetValue(3.ToString();
IEnumerable result=service.executeemotewithrunid(“检查当前执行”,转换为32(当前运行ID));
返回结果;
}捕获(例外情况除外)
{throw ex;}
}
}
不过,如果服务器上正在运行ajax调用,我不知道如何打开/重定向页面URL。我也在同一页中使用信号器。请提供帮助。如果您的url/链接重定向到其他页面,则浏览器将停止所有呼叫itself@Vivek. 是的,我想要同样的东西。如果我点击锚定标签,那么同时所有请求都应该被停止,我的页面应该被重定向到所需的URL。你可以通过打开网络选项卡在chrome上检查同样的情况:)@Vivek当前浏览器没有重定向我的URL,它在浏览器顶部显示加载标志,当在UI上检索ajax请求数据时,然后页面重定向。