考虑ajax请求:
var idNumber = $("#Field_1").val();
var servletUrl = "someURL"; // some url goes here
$.ajax({
url: servletUrl,
type: 'POST',
cache: false,
data: { },
success: function(data){
alert(data);
window.location = "./replyToYou.html";
}
, error: function(jqXHR, textStatus, err){
alert('text status '+textStatus+', err '+err + " " + JSON.stringify(jqXHR));
}
});
成功时如何将 , 传递给 replyToYou.html
,以及如何在 replyToYou.html
中获取它?
非常感谢
最佳答案
你必须有不同的解决方案:
1-首先使用queryString:
success: function(data){
alert(data);
window.location = "./replyToYou.html?idNumber=" + idNumber;
}
然后使用此函数从查询字符串中读取它(我将其用作最初在此 POST 中创建的查询字符串助手):
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
getParameterByName("idNumber");
2-另一个选项是使用localStorage
:
success: function(data){
alert(data);
localStorage.setItem("idNumber",idNumber);
window.location = "./replyToYou.html";
}
并在其他页面获取它,例如:
localStorage.getItem("idNumber")
关于javascript - ajax成功后如何将变量从JS代码传递到HTML页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655887/