Two solutions to open a new window in Ajax that is blocked by the browser:
Recently, when I made a payment, I found that I was intercepted by the browser when I opened the payment window. Baidu only found that it was because I used ajax to verify whether I could pay before opening the window, therefore, the ixin window is not automatically opened by the user. The browser deems it unsafe and blocks the message.
Solution 1
Start to open a new window, and then change the url of the new window. The specific code is
Var wd = window. open (); $. ajax ({type: "POST", dataType: "json", url: URL, data: {orderNo: orderNo}, success: function (data) {if (data. status = 'success') {wd. location. href = 'HTTP: // www.baidu.com ';} else {alert ('order cannot be paid! ') ;}}, Error: function (data) {alert ("loading... please wait! ");}});
This implementation method has the disadvantage of executing var wd = window regardless of whether ajax is successful or not. open (); this line of code opens a new window for both success and failure, unless it is closed after failure, but the user experience will be very poor, so I used the second method to implement it.
Solution 2
Because ajax is asynchronous by default, so high performance and good user experience, but this also leads to security issues, so the browser should think that the new window is safe, all ajax files must be synchronized before the new window is popped up. The specific code is
$. Ajax ({type: "POST", dataType: "json", url: URL, async: false, // synchronous request data: {orderNo: orderNo}, success: function (data) {if (data. status = 'success') {window. open ("www.baidu.com");} else {alert ('order cannot be paid! ') ;}}, Error: function (data) {alert ("loading... please wait! ");}});
The above is a small Editor to introduce you to the Ajax open a new window by the browser blocked two solutions, I hope to help you, if you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!