Home >Web Front-end >JS Tutorial >Effective method to open a new form with ajax callback to prevent browser interception_AJAX related
This article mainly introduces in detail the effective method of opening a new form with ajax callback to prevent browser interception. Interested friends can refer to
ajax callback to open a new form to prevent browser interception. Just do it!
Problem analysis:
##
function click_fun(){ window.open("www.baidu.com");//能打开 $.ajax({ 'url': '${pageContext.request.contextPath}/activity/savePrizes.htm', 'type': 'post', 'dataType': 'json', 'data': data, success: function (data) { window.open("www.baidu.com");//被拦截 }, error:function(){ } }); }
Analysis: Open new The form can only be triggered within the click event. Opening the form within the callback function within the click event will be intercepted, and the browser will think it is code such as advertising pop-ups
Solution 1 :
function click_fun_new(){ var tempwindow=window.open();//先打开临时窗体,由于是点击事件内触发,不会被拦截 $.ajax({ 'url': '${pageContext.request.contextPath}/activity/savePrizes.htm', 'type': 'post', 'dataType': 'json', 'data': data, success: function (data) { tempwindow.location = "www.baidu.com";//当回调的时候更改临时窗体的路径 }, error:function(){ tempwindow.close();//回调发现无需打开窗体时可以关闭之前的临时窗体 } }); }
##Solution 2:
function click_fun_new(){ var flag = false; $.ajax({ 'url': '${pageContext.request.contextPath}/activity/savePrizes.htm', 'type': 'post', 'dataType': 'json', 'data': data, 'async':false,//同步请求 success: function (data) { $("#a").attr("href","www.baidu.com");//当回调的时候更改页面上或创建的某个a标签的href flag = true;//更改标志 }, error:function(){ } }); if(flag){ $("#a").click();//href属性更改后模拟点击 } }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Very practical ajax user registration moduleAjax clicks to continuously load the data list (graphic tutorial )Ajax Struts2 implements verification code verification function (graphic tutorial)The above is the detailed content of Effective method to open a new form with ajax callback to prevent browser interception_AJAX related. For more information, please follow other related articles on the PHP Chinese website!