问题
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