Open a new window after successful Ajax request window.open () solution to interception

Time:2021-4-18

Question:

The previous development project encountered a problem, AJAX asynchronous request after the success of the need to open a new window to open the URL, using the window.open () method, but unfortunately it was intercepted by the browser. How to solve this problem?
analysis:

The reason why the browser intercepts a new window is that the operation is not triggered by the user, so it thinks it is unsafe and intercepts it (if it is)_ Even if the Ajax callback function simulates the click or submit user behavior (trigger (‘click ‘)), the browser will consider that it is not triggered by the user, so it cannot be executed safely, so it is intercepted.
resolvent:

1. Asynchronous to synchronous, that is: async:false

2. Point the new window to an object, and then modify the URL of the object


$('.task').bind('click',function(){
var w = window.open();
$.ajax({
type: 'POST',
url: '/surveyTask',
dataType: 'json',
error: function(){
w.close();
},
success: function(res){
w.location = res.url;
}
});
});

Finally, it should be noted that the method of dynamically adding a new form window on the Internet is not suitable for Ajax requests. At least the blogger has been intercepted by the browser after testing.