Ajax - 在 "success" 之后转到另一个页面,从那里的 url 检索用户 ID
Ajax - after "success" go to another page retrieve user id from the url there
var JSONonj = {"name": "James","age":"25"};
var data = "This data i would like to retrieve and print in the main.html page after success call in ajax.";
$.ajax({
url: "/user",
type: "POST",
data: JSONobj,
dataType: "json",
success: function() {
window.location.href = "main.html";
},
contentType: "application/json"
});
这是交易。成功后我想重定向到 main.html ,当我到达那里时,我想在那里检索和打印数据变量。
重定向工作正常。但是我无法在那里接收数据。
您必须在查询字符串中发送该数据并使用 javascript 重建它。或者你将不得不 POST 到 handler/controller (谁知道你在使用什么..)与 json 数据并重建它......如果你要改变的话,无论哪种方式您必须发送数据的位置。
如果您正在使用 HTML5,那么按照 LeGEC 的建议,本地存储将是一个不错的选择。
Storing Objects in HTML5 localStorage
如果您使用 jQuery,可以应用以下函数。
var redirect = 'http://www.example.com/';
$.redirectPost(redirect, {x: 'exemplo', y: '123'});
// jquery extend function
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
value = value.split('"').join('\"')
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
主要有两种方法可以实现:
- 以某种方式将数据发送到服务器(有关可能的方法,请参阅其他答案)
使用浏览器的localStorage
:
success: function() {
localStorage.setItem('myMainKey', data);
window.location.href = "main.html";
}
// in main.html's javascript :
var data = localStorage.getItem('myMainKey');
if (data !== undefined) {
//do something
}
注意:如果要存储和检索完整的 javascript 对象,则必须使用 JSON.stringify()
/ JSON.parse()
来存储/检索它。
你也可以这样做:-
window.location.href="main.html?data="+data;
在main.html
var user = decodeURI(getUrlVars()["src"]);
`//Now do some stuff with this data`
`function getUrlVars() {
var vars = [], hash;
var hashes =
window.location.href.slice(window.location.href.indexOf('?') +
1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}`
var JSONonj = {"name": "James","age":"25"};
var data = "This data i would like to retrieve and print in the main.html page after success call in ajax.";
$.ajax({
url: "/user",
type: "POST",
data: JSONobj,
dataType: "json",
success: function() {
window.location.href = "main.html";
},
contentType: "application/json"
});
这是交易。成功后我想重定向到 main.html ,当我到达那里时,我想在那里检索和打印数据变量。
重定向工作正常。但是我无法在那里接收数据。
您必须在查询字符串中发送该数据并使用 javascript 重建它。或者你将不得不 POST 到 handler/controller (谁知道你在使用什么..)与 json 数据并重建它......如果你要改变的话,无论哪种方式您必须发送数据的位置。
如果您正在使用 HTML5,那么按照 LeGEC 的建议,本地存储将是一个不错的选择。
Storing Objects in HTML5 localStorage
如果您使用 jQuery,可以应用以下函数。
var redirect = 'http://www.example.com/';
$.redirectPost(redirect, {x: 'exemplo', y: '123'});
// jquery extend function
$.extend(
{
redirectPost: function(location, args)
{
var form = '';
$.each( args, function( key, value ) {
value = value.split('"').join('\"')
form += '<input type="hidden" name="'+key+'" value="'+value+'">';
});
$('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
}
});
主要有两种方法可以实现:
- 以某种方式将数据发送到服务器(有关可能的方法,请参阅其他答案)
使用浏览器的
localStorage
:success: function() { localStorage.setItem('myMainKey', data); window.location.href = "main.html"; } // in main.html's javascript : var data = localStorage.getItem('myMainKey'); if (data !== undefined) { //do something }
注意:如果要存储和检索完整的 javascript 对象,则必须使用 JSON.stringify()
/ JSON.parse()
来存储/检索它。
你也可以这样做:-
window.location.href="main.html?data="+data;
在main.html
var user = decodeURI(getUrlVars()["src"]);
`//Now do some stuff with this data`
`function getUrlVars() {
var vars = [], hash;
var hashes =
window.location.href.slice(window.location.href.indexOf('?') +
1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}`