Javascript 通过html页面解析JSON数据
我得到一个JSON格式的字符串,我想把它发送到profile.html 我的问题是如何保存字符串,并使用从JSON获取的变量将其解析到profile.html页面,因为我需要从一页到另一页的会话和用户id变量。 当我调试时,调试程序说中的“user_id”未定义; 谢谢,非常感谢Javascript 通过html页面解析JSON数据,javascript,json,parsing,variables,Javascript,Json,Parsing,Variables,我得到一个JSON格式的字符串,我想把它发送到profile.html 我的问题是如何保存字符串,并使用从JSON获取的变量将其解析到profile.html页面,因为我需要从一页到另一页的会话和用户id变量。 当我调试时,调试程序说中的“user_id”未定义; 谢谢,非常感谢 enter code here gloabl variables are User_id,Session; var params ={ username: document.getElement
enter code here
gloabl variables are User_id,Session;
var params ={
username: document.getElementById('usernameId').value,
password: document.getElementById('passwordId').value,
rememberMe :document.getElementById('rememberId').value,
};
login(params, function(data){
var account=JSON.stringify(data);
account=JSON.parse(account);
if(account.success==true){
User_id=account.user_id;
session=account.session;
window.location='profile.html';
}
enter code here
代码与版本:
login(params, function(data)
{
var account=JSON.parse(data); // this serve the purpose
if(account.success==true){
var value= account.user_id + ', ' + account.session;
// now you can either pass it as query string or as parameter to your destination page.
window.location='profile.html';
}
您是否将用户id和会话定义为变量 我认为account.user\u id中没有设置。请检查Json。输入一些静态数据并首先进行检查。然后检查json。你能给你显示json字符串吗…{“user_id”:“123456”,“session”:“abcdefg”}使用静态数据它工作正常。但我想动态地获取数据。显然JSON数据有问题。您可以通过browser console.log进行检查。您需要在account对象中设置用户id。请把它调好。它会自动工作。如果您需要任何帮助,请共享代码。检查登录中的数据。您可以使用alert(data.toSource())检查它的属性。我需要检查数据中的变量及其值。如果(account.success==true){save(account.user\u id,account.session);window.location='profile.html';….var user\u id;var session;函数save(u,s){user\u id=u;session=s;}但调试器仍然显示:用户id和会话未定义。如果帐户中有数据。用户id和会话。请使用一些静态值进行检查。请重试一次。除此之外,我在代码中未发现任何问题。如果可能,尝试共享整个声明,将检查一次。