将Async="true"添加到ASMX中的Web方法,并在jQuery Ajax请求中添加async: true选项。
示例代码:
ASMX中的Web方法:
[WebMethod] [ScriptMethod(UseHttpGet = true)] public void MyWebMethod(string param1) { // Some code here }
更改为异步方法:
[WebMethod] [ScriptMethod(UseHttpGet = true, Async = true)] public IAsyncResult BeginMyWebMethod(string param1, AsyncCallback callback, object asyncState) { // Some code here }
[WebMethod] [ScriptMethod(UseHttpGet = true, Async = true)] public void EndMyWebMethod(IAsyncResult result) { // Some code here }
在jQuery中添加async选项:
$.ajax({ url: "MyWebService.asmx/MyWebMethod", data: { param1: "value1" }, type: "GET", contentType: "application/json; charset=utf-8", dataType: "json", async: true, success: function (response) { // Some code here }, error: function (xhr, textStatus, errorThrown) { // Some code here } });