本文介绍了ASP.NET页面的WebMethod AJAX调用请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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秒
}
 

这是用于不没有设置自己的超时的所有请求。从服务器的角度来看,有一个同步或异步请求之间没有差别的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
}

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.

这篇关于ASP.NET页面的WebMethod AJAX调用请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:55