我在這個問題上陷入了相當長的一段時間。建議需要...使用AJAX和.net Web服務進行異步處理
好吧,我有這個ASPX測試服務頁面(AJAX)
<script type="text/javascript">
$(document).ready(function() {
$('#btnTest').click(function() {
$('#btnTest').hide();
$.ajax({
type: 'POST',
url: "request.asmx/newSync",
data: '',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
complete: function() {
$('#btnTest').show();
},
failure: function (msg) {
$('#result').text(msg);
//alert(msg);
}
});
});
});
</script>
Web服務
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class test_request : System.Web.Services.WebService
{
public test_request() {
}
[WebMethod(EnableSession = true)]
public SyncAsync newSync()
{
var response = new SyncAsync();
//Test Processing
System.Threading.Thread.Sleep(5000);
try
{
response.Status = "Done";
return response;
}
catch (Exception ex)
{
response.Status = "error";
response.ErrorMessage = ex.Message;
ErrorSignal.FromCurrentContext().Raise(ex);
}
return response;
}
}
好我的問題是這樣的......這是異步? ...意思是當我點擊它發送請求的按鈕時,我不會等待web服務返回的任何響應...?
有人可以解釋一下嗎?
謝謝
你javascipr是異步,它不會發送請求塊並在響應返回,將火'complete'或'failure'回調。 – 2014-12-05 11:27:01
注意它是'錯誤:'而不是'失敗:'對於任何ajax錯誤。 – 2014-12-05 11:28:57