更新時間:2021-12-01 09:30:08 來源:動力節(jié)點 瀏覽2187次
如何進行AJAX跳轉(zhuǎn)和跳轉(zhuǎn)到新窗口?
一般我們在這個頁面跳轉(zhuǎn),直接window.location.href = yourjumpurl;
但是如果你想跳轉(zhuǎn)到新頁面,你需要這樣做:
①:var newWin = window.open('_blank');
②:$.ajax( .. . . ... .,
success:function (data) {
/ * Suppose data.url is the address to be jumped * /
newWin.location.href = data.url;
/ * Hereby Note that the new window is outside Ajax instead of the Success callback inside * /
}
);
1.ajax只接受最后返回的值,不會響應(yīng)跳轉(zhuǎn)請求更改瀏覽器地址欄地址轉(zhuǎn)向的,你需要用js判斷ajax的返回值是否要跳轉(zhuǎn),然后設(shè)置location.href實現(xiàn)跳轉(zhuǎn)。
2.ajax異步請求struts的action只會返回所請求頁面的html源代碼,這樣請求是不會跳轉(zhuǎn)的,這種用法只是在替換頁面局部html時使用。
3.在springMVC框架中,當controller層方法返回String類型的時候默認是進行頁面跳轉(zhuǎn),這時候后臺使用return時ajax接收到的并不是后臺返回的某個字符串或狀態(tài)碼,而是整個html對象,這時可以在后臺的方法上添加注解 @ResponseBody。
4.無法從ajax函數(shù)外部獲取ajax請求到的數(shù)據(jù),在需要使用數(shù)據(jù)的組件之前,先在ajax回調(diào)函數(shù)中使用localstorage.setItem()將數(shù)據(jù)儲存在本地,在組件中使用localstorage.getItem()調(diào)用。
在此過程中嘗試在componentWillMount 中用 setState 來傳遞數(shù)據(jù),但是報錯,錯誤的大致內(nèi)容是 setSate 必須在component 的 mounting和mounted狀態(tài)下才能使用。
通過上述相信大家對AJAX跳轉(zhuǎn)和跳轉(zhuǎn)到新窗口的方法已經(jīng)有所了解,大家若想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點的Java在線學習,里面的課程由淺到深,適合小白學習,希望對大家能夠有所幫助。