ajax提交中新標(biāo)簽頁(yè)打開(kāi)響應(yīng)返回的url
-
錯(cuò)誤處理方式:
##觸發(fā)按鈕 <a href="javascrip:void();" data-url="xxxx" id='jiedong' class='tixian'>解凍</a> ##綁定事件 $("#jiedong").click(function (e) { $this = $(this); $this.text("處理中..."); $.ajax({ url: "xxxxxx", type: 'get', data: {}, success: function(data){ $this.text("解凍"); if(data.status == 1) { #新標(biāo)簽打開(kāi)頁(yè)面 window.open($this.attr("data-url"); } else { ..... } }, error: function () { alert("服務(wù)器異常"); } }); }); -
原因分析及處理方案:
- 原因:
使用window.open打開(kāi)url,瀏覽器會(huì)將該請(qǐng)求當(dāng)成非用戶(hù)操作。
當(dāng)瀏覽器檢測(cè)到非用戶(hù)操作產(chǎn)生的新彈出窗口,則會(huì)對(duì)其進(jìn)行阻止。因?yàn)闉g覽器認(rèn)為這不是一個(gè)用戶(hù)希望看到的頁(yè)面。 - 處理方案:
原a標(biāo)簽正常執(zhí)行新開(kāi)頁(yè)面操作,只是生效時(shí)間放到執(zhí)行click操作之后,即:a標(biāo)簽綁定data-remote=”true“屬性,延遲執(zhí)行超鏈接動(dòng)作,當(dāng)click返回true時(shí)才執(zhí)行href的鏈接。
注意:此時(shí)ajax提交要進(jìn)行同步處理,不然超鏈接是不會(huì)等待ajax的請(qǐng)求處理完才執(zhí)行了跳轉(zhuǎn)操作。(默認(rèn)是異步處理,所以需要設(shè)置async:true;)
- 原因:
-
修正后處理方式:
##觸發(fā)按鈕 <a href="xxxx" id='jiedong' class='tixian' data-remote="true" target="_blank">解凍</a> ## 綁定事件 $("#jiedong").click(function (e) { $this = $(this); ## 設(shè)置標(biāo)桿,便于ajax的處理結(jié)果向外滲透 var flag = false; $this.text("處理中..."); $.ajax({ url: "xxxxxx", type: 'get', async: false, ## 重要 data: {}, success: function(data){ $this.text("解凍"); if(data.status == 1) { flag = true; ##向外滲透結(jié)果 } else { ..... } }, error: function () { alert("服務(wù)器異常"); } }); ## 決定是否執(zhí)行超鏈接動(dòng)作 return flag; });