ASP.NET页面的WebMethod AJAX调用请求超时页面、NET、ASP、WebMethod

2023-09-10 17:50:30 作者:细数那些回不去的记忆丶

我的webmethod的ASP页

  [WebMethod的]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)
    公共静态字符串的MyMethod(字符串requestObject)
    {
        //这里的操作,需要大约200  - 300秒
    }
 

也有是页面上的AJAX方法

  jQuery.ajax({
        网址:MyPage.aspx /的MyMethod,
        键入:POST,
        的contentType:应用/ JSON
        数据类型:JSON,
        数据:somedata,
        超时:300000,

        成功:函数(响应){
            一些处理程序
        }
    });
 

当我试图把这种阿贾克斯方法得到System.Web.HttpException:请求超时

我尝试添加executionTimeout =300为元素在web.config中。 而问题解决了。但据我了解,这将增加超时的所有应用程序。 我不想这样做。 有没有更合适的方式来解决超时异常? 又请问executionTimeout参数设置超时时间,所有请求,或只为异步请求?

解决方案

您是否尝试过 Server.ScriptTimeout

  [WebMethod的]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)
公共静态字符串的MyMethod(字符串requestObject)
{
    HttpContext.Current.Server.ScriptTimeout = 300;
    //这里的操作,需要大约200  - 300秒
}
 
asp.net webservice如何获取jquery ajax post请求的参数

  

和做executionTimeout参数设置超时时间,所有请求或   只是异步请求?

这是用于不没有设置自己的超时的所有请求。 从服务器的角度来看,有一个同步或异步请求之间没有差别的http

I have webmethod on asp page

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string MyMethod(string requestObject)
    {
        // here is operation that takes approximately 200 - 300 second
    }

Also there is an AJAX method on page

    jQuery.ajax({
        url: 'MyPage.aspx/MyMethod',
        type: 'POST',
        contentType: "application/json",
        dataType: 'json',
        data: somedata,
        timeout: 300000,

        success: function (response) {
            some handler
        }
    });

When I try to call this ajax method I get 'System.Web.HttpException: Request timed out.'

I tried to add executionTimeout="300" to element in web.config. And issue is resolved. But as I understand this will increase timeouts for all application. I don't want to do this. Is there a more proper way to fix timeout exception? And does executionTimeout parameter set timeout for all requests or just for async requests?

解决方案

Have you tried Server.ScriptTimeout ?

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string MyMethod(string requestObject)
{
    HttpContext.Current.Server.ScriptTimeout = 300;
    // here is operation that takes approximately 200 - 300 second
}

And does executionTimeout parameter set timeout for all requests or just for async requests?

This is for all requests which do no set their own timeout. From the server point of view, there is no http difference between a sync or async request.