我有以下代码..但旋转效果在ajax调用完成之前不会发生(这几乎没用)
$.ajax({
type:"GET",
async:false,
url: "../../xxxxx/AvailabilityServlet",
data: {action: "xxx", sku: sku},
beforeSend: function( xhr ) {
$("#submiticon").removeClass("fa-search");
$("#submiticon").addClass("fa-spinner fa-spin");
}
})
.done(function( data ) {
result = data
})
.fail(function( data ) {
result=null
})
.always(function( data ) {
$("#submiticon").addClass("fa-search");
$("#submiticon").removeClass("fa-spinner");
$("#submiticon").removeClass("fa-spin");
})
但如果我添加警报,它会抛出警报,然后旋转效果开始显示。
$.ajax({
type:"GET",
async:false,
url: "../../xxxxx/AvailabilityServlet",
data: {action: "xxx", sku: sku},
beforeSend: function( xhr ) {
$("#submiticon").removeClass("fa-search");
$("#submiticon").addClass("fa-spinner fa-spin");
alert("stop") ;
}
JSP:
<div class="col-md-4">
<form role="form" class="ng-pristine ng-valid">
<div class="input-group col-md-6" >
<input type="text" class="form-control" id="skuInput" placeholder="Sku" autocomplete="off" >
<span id="submit" class="input-group-addon"><span id="submiticon" class="fa fa-search"></span></span>
</div>
</form>
</div>
$("#submit").click(function(){
searchSku()
});
答案 0 :(得分:2)
从async:false
电话中移除$.ajax
。您正在使它同步而不是异步(AJAX中的A)并在等待响应时挂起浏览器。