3 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
window.open
window.open
后來(lái)$.getJSON
做點(diǎn)別的事,而不是 window.open
.讓Ajax調(diào)用是同步的,這是您通常應(yīng)該避免的事情,因?yàn)樗i定了瀏覽器的UI。 $.getJSON
相當(dāng)于: $.ajax({ url: url, dataType: 'json', data: data, success: callback});
.這樣你就可以 $.getJSON
通過(guò)將Params映射到上面并添加 async: false
:$.ajax({ url: "redirect/" + pageId, async: false, dataType: "json", data: {}, success: function(status) { if (status == null) { alert("Error in verifying the status."); } else if(!status) { $("#agreement").dialog("open"); } else { window.open(redirectionURL); } }});
同樣,如果您能夠找到實(shí)現(xiàn)目標(biāo)的其他方法,我也不提倡同步Ajax調(diào)用。但如果你做不到,那就去吧。 下面是由于異步調(diào)用導(dǎo)致測(cè)試失敗的代碼示例:
(由于對(duì)JSBin的更改,活動(dòng)鏈接不再工作)
jQuery(function($) { // This version doesn't work, because the window.open is // not during the event processing $("#theButton").click(function(e) { e.preventDefault(); $.getJSON("http://jsbin.com/uriyip", function() { window.open("http://jsbin.com/ubiqev"); }); });});
(由于對(duì)JSBin的更改,活動(dòng)鏈接不再工作)
jQuery(function($) { // This version does work, because the window.open is // during the event processing. But it uses a synchronous // ajax call, locking up the browser UI while the call is // in progress. $("#theButton").click(function(e) { e.preventDefault(); $.ajax({ url: "http://jsbin.com/uriyip", async: false, dataType: "json", success: function() { window.open("http://jsbin.com/ubiqev"); } }); });});

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
$scope.testCode = function () { var newWin = $window.open('', '_blank'); service.testCode().then(function (data) { $scope.testing = true; newWin.location = '/Tests/' + data.url.replace(/["]/g, ""); });};
- 3 回答
- 0 關(guān)注
- 514 瀏覽
添加回答
舉報(bào)