[英]Save the jQuery Ajax Response in global variable to send it to another page (no local or session storage)
我正在嘗試進行Ajax調用並將響應保存在可以在另一個頁面中訪問的全局變量中。
這是我可重用的AJAX函數-
function coAjax(url, data, handleData) {
if(url != null && url != ''){
$.ajax({
url:url,
type:'POST',
dataType: 'json',
contentType: 'application/json',
data: data,
success:function(result) {
handleData(result);
},
error: function(e){
console.log('error');
}
});
}};
我在單擊頁面1的按鈕上使用了該功能-
$('#buttonOnPage1').click(function(e){
e.preventDefault();
var url ="example.com";
var data = JSON.stringify(dummyData);
coAjax(url, data, function(result){
console.log(result);
window.location.href = "page2.jsp";
});
}
我希望在不使用可以在page2.jsp
使用的local
和session storage
情況下訪問ajax的result
。 我無法說明不使用本地存儲和會話存儲的原因。 請幫忙。
該數據將作為page2.jsp可用:<%= request.getParameter(“ json_data_id”)%>
如果data =({data:[{name:“ John”,pet:“ cat”},{name:“ Doe”,time:“ dog”}]}),則“ json_data_id”將是:data [0] [名稱]
您可以編碼encodeURIComponent
您的數據,並使用它當作查詢參數的新頁面的URL和訪問window.location.search
和解碼decodeURIComponent
那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.