ASP.NET page webmethod AJAX call Request timed out

那年仲夏 提交于 2019-12-08 01:47:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17040695/asp-net-page-webmethod-ajax-call-request-timed-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!