ASP.NET MVC Ajax请求错误消息

时间:2022-10-07 16:27:01

I'm using ASP.NET MVC 5 & C#. Basically when I click a button on my website, it should generate a report in async manner. Generating report takes ~10 minutes. Also while the report is being generated asynchronously, I need to be able to keep on using other area of the website.

我正在使用ASP.NET MVC 5和C#。基本上当我点击我网站上的按钮时,它应该以异步方式生成报告。生成报告大约需要10分钟。同时,报告是异步生成的,我需要能够继续使用网站的其他区域。

So I have mainly below look alike ajax request that I call on generate report button click. I also have my async action method in place properly. If I hit the generate report button then it starts processing it and display "Report generated!" once the report is generated (like in 10 minutes).

所以我主要在下面看起来像ajax请求,我调用生成报告按钮单击。我也正确地使用了异步操作方法。如果我点击生成报告按钮,它会开始处理它并显示“生成报告!”生成报告后(如10分钟内)。

That's fine. But after clicking the button, if I try to access any other area on the website (like clicking on other tab/button/etc) then it immediately displays me the error message "An error occurred!", which is coming from ajax error section - See below code. Also it completes the report generation asynchronously fine. But it just shows me the error message right after I hit the button!

没关系。但点击按钮后,如果我尝试访问网站上的任何其他区域(如点击其他选项卡/按钮/等),它会立即显示错误消息“发生错误!”,这是来自ajax错误部分 - 见下面的代码。它还可以异步完成报告生成。但它只是在我按下按钮后立即向我显示错误消息!

This seems weird to me. Please guide me as what's happening here and help me to fix this. Thanks.

这对我来说似乎很奇怪。请指导我这里发生的事情并帮助我解决这个问题。谢谢。

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: function(html){
    alert("Report generated!");
  },
  error: function(e) {
     alert("An error occurred!");
     }
});

1 个解决方案

#1


0  

Javascript code runs within the context of the page. When you try to navigate around your website the current page will be unloaded and the javascript objects for the page will be destroyed. This is what is triggering the error callback, the ajax object knows the page is being unloaded and letting you know that the call failed before it is destroyed.

Javascript代码在页面上下文中运行。当您尝试浏览网站时,将卸载当前页面,并且将破坏页面的javascript对象。这就是触发错误回调的原因,ajax对象知道页面正在被卸载,并让你知道调用在被销毁之前失败了。

To solve your problem you should look into generating the report on a background thread on the server and have a separate page which can poll for the status of the report and allow the user to download it when finished.

要解决您的问题,您应该考虑在服务器上的后台线程上生成报告,并有一个单独的页面,可以轮询报告的状态,并允许用户在完成后下载它。

#1


0  

Javascript code runs within the context of the page. When you try to navigate around your website the current page will be unloaded and the javascript objects for the page will be destroyed. This is what is triggering the error callback, the ajax object knows the page is being unloaded and letting you know that the call failed before it is destroyed.

Javascript代码在页面上下文中运行。当您尝试浏览网站时,将卸载当前页面,并且将破坏页面的javascript对象。这就是触发错误回调的原因,ajax对象知道页面正在被卸载,并让你知道调用在被销毁之前失败了。

To solve your problem you should look into generating the report on a background thread on the server and have a separate page which can poll for the status of the report and allow the user to download it when finished.

要解决您的问题,您应该考虑在服务器上的后台线程上生成报告,并有一个单独的页面,可以轮询报告的状态,并允许用户在完成后下载它。