Home > Web Front-end > JS Tutorial > How to avoid being intercepted by the browser when opening a new form with ajax callback

How to avoid being intercepted by the browser when opening a new form with ajax callback

php中世界最好的语言
Release: 2018-04-04 15:40:24
Original
1740 people have browsed it

This time I will show you how to avoid being intercepted by the browser when opening a new form with ajax callback. How to avoid being intercepted by the browser when opening a new form with ajax callback. What are the precautions?. Here is the actual combat. Let’s take a look at the case.

ajax callback opens a new form to prevent browser interception, just do this!

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(){ 
 
    } 
  }); 
}
Copy after login

Analysis:
Opening a new form can only be triggered within the click event , the form opened 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();//回调发现无需打开窗体时可以关闭之前的临时窗体 
    } 
  }); 
}
Copy after login

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属性更改后模拟点击 
  } 
}
Copy after login
The above are two methods for opening a new form with ajax callback to prevent browser interception. I hope it will be helpful to everyone's study.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Jsonp solves the cross-domain problem of ajax

How does JSONP handle the cross-domain access of Ajax

The above is the detailed content of How to avoid being intercepted by the browser when opening a new form with ajax callback. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template