This time I will bring you the window.openhow to handle the interception when calling window.open in the ajax request. What are the precautions?What are the following? This is a practical case, let’s take a look at it.
Description of the advantages of this method: For example, if ajaxcallback function fails, the empty link opened by the browser by default will be automatically closed, and the user experience is better.
var newwin; $("#btn").click(function(){ newwim = window.open(); //此处调用ajax,异步即可: $.ajax({ type:'POST', url: ajaxurl, dataType : 'json', data:parameter, beforeSend : function(){}, success:function(data) { if(data.TSR_CODE == "0"){ newwin.location.href = xxx;//xxx为后端返回的地址; }else{ alert(data.TSR_MSG); newwin.close();//此处为ajax失败方法,自动关闭刚才会打开的页面; } }, error:function(){ console.log("访问出错,请重试!"); }, complete:function(){} }); });
Analysis of the second solution:
This method has been tested: for example, the background returns outside the site For URLs, Google Chrome, 360 Browser, etc. will intercept requests, but Firefox and QQ browsers will not. It is predicted that the browser itself intercepts off-site URLs of this type, and ordinary URLs will not be intercepted;
$("#btn").click(function(){ //此处调用ajax,异步即可: $.ajax({ type:'POST', url: ajaxurl, dataType : 'json', async:false,//改为同步请求 data:parameter, beforeSend : function(){}, success:function(data) { if(data.TSR_CODE == "0"){ window.open(xxx); }else{ alert(data.TSR_MSG); } }, error:function(){ console.log("访问出错,请重试!"); }, complete:function(){} });
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese websiteOther related articles!
Recommended reading:
JSON and Math use case analysis in JS
Detailed explanation of the implementation steps of PromiseA
The above is the detailed content of How to deal with interception when calling window.open in ajax request. For more information, please follow other related articles on the PHP Chinese website!