Can';t更新javaScript全局变量
这里有一个全局变量Can';t更新javaScript全局变量,javascript,jquery,html,asynchronous,Javascript,Jquery,Html,Asynchronous,这里有一个全局变量userId,我想在signInUserFunction()中更新它,以便在其他函数中使用is。我曾尝试使用var,window来定义它,但所有这些都没有帮助。此变量不更新。正如我看到的,它是关于AJAX异步的。那么,我能用它做什么呢 是的,我知道用JS进行身份验证是不好的,我对它很陌生。所以,我只是创建随机方法来改进 var userId = 1; function signInUser() { $.getJSON('http://localhost:8887/JAXR
userId
,我想在signInUserFunction()中更新它,以便在其他函数中使用is。我曾尝试使用var
,window
来定义它,但所有这些都没有帮助。此变量不更新。正如我看到的,它是关于AJAX异步的。那么,我能用它做什么呢
是的,我知道用JS进行身份验证是不好的,我对它很陌生。所以,我只是创建随机方法来改进
var userId = 1;
function signInUser() {
$.getJSON('http://localhost:8887/JAXRSService/webresources/generic/getAllUsers', function(data) {
var items = [];
var i = 0;
$.each(data, function(firstname, value) {
var str = JSON.stringify(value);
data = JSON.parse(str);
var innerId;
for (p in data) {
innerId = data[p].id;
if ($('#nameSignIn').val() == data[p].first_name && $('#passwordSignIn').val() == data[p].password) { //
userId = innerId;
window.location.href = "content.html";
break;
} else {
i++;
if (i == data.length) {
alert("Ощибка в логине или пароле!")
}
}
}
});
});
}
您如何确定它是否已设置?看起来,在设置之后,您会立即导航到另一个页面。当你到达那个页面时,你将有一个全新的窗口
尝试在导航离开之前警告该值
编辑:以下是如何将其传递到其他页面(但在真正的应用程序中不应这样做)
然后在另一个页面中,您可以从文档中访问它。位置:
alert(document.location.toString().split("?id=")[1]);
您如何确定它是否已设置?看起来,在设置之后,您会立即导航到另一个页面。当你到达那个页面时,你将有一个全新的窗口
尝试在导航离开之前警告该值
编辑:以下是如何将其传递到其他页面(但在真正的应用程序中不应这样做)
然后在另一个页面中,您可以从文档中访问它。位置:
alert(document.location.toString().split("?id=")[1]);
使用@mcgraphix提出的想法(并向您发出同样的警告……这肯定不是在生产环境中传输此类数据的方式),这里有一种方法:
function signInUser() {
var url = 'http://localhost:8887/JAXRSService/webresources/generic/getAllUsers';
var userId;
$.getJSON(url, function(data) {
$.each(data.Actors, function(index, actor) {
// Cache the values of the #nameSignIn and #passwordSignIn elements
var name = $('#nameSignIn').val();
var password = $('#passwordSignIn').val();
if (actor.first_name === name && actor.password === password) {
// We have found the correct actor.
// Extract its ID and assign it to userId.
userId = actor.id;
window.location.href = "content.html?userId=" + userId;
}
});
// This alert should only be reached if none of the actor objects
// has a name and password that matches your input box values.
alert("Ощибка в логине или пароле!");
});
}
// On the next page...
// Top answer from http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
// This approach can handle URLs with more than one query parameter,
// which you may potentially add in the future.
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
}
var userId = getQueryVariable('userId');
函数签名器(){
var url='1〕http://localhost:8887/JAXRSService/webresources/generic/getAllUsers';
var用户id;
$.getJSON(url、函数(数据){
$.each(data.Actors,function(index,actor){
//缓存#nameSignIn和#passwordSignIn元素的值
var name=$('#nameSignIn').val();
var password=$('#passwordSignIn').val();
if(actor.first_name==name&&actor.password==password){
//我们找到了合适的演员。
//提取其ID并将其分配给userId。
userId=actor.id;
window.location.href=“content.html?userId=“+userId;
}
});
//只有在没有任何actor对象的情况下才应达到此警报
//具有与输入框值匹配的名称和密码。
警惕(“бббббббббббббббб107;
});
}
//在下一页。。。
//来自http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
//此方法可以处理具有多个查询参数的URL,
//您可能会在将来添加。
函数getQueryVariable(变量){
var query=window.location.search.substring(1);
var vars=query.split('&');
对于(变量i=0;i
使用@mcgraphix提出的想法(并给你同样的警告…在生产环境中,这肯定不是这样传输数据的方式),这里有一种方法:
function signInUser() {
var url = 'http://localhost:8887/JAXRSService/webresources/generic/getAllUsers';
var userId;
$.getJSON(url, function(data) {
$.each(data.Actors, function(index, actor) {
// Cache the values of the #nameSignIn and #passwordSignIn elements
var name = $('#nameSignIn').val();
var password = $('#passwordSignIn').val();
if (actor.first_name === name && actor.password === password) {
// We have found the correct actor.
// Extract its ID and assign it to userId.
userId = actor.id;
window.location.href = "content.html?userId=" + userId;
}
});
// This alert should only be reached if none of the actor objects
// has a name and password that matches your input box values.
alert("Ощибка в логине или пароле!");
});
}
// On the next page...
// Top answer from http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
// This approach can handle URLs with more than one query parameter,
// which you may potentially add in the future.
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
}
var userId = getQueryVariable('userId');
函数签名器(){
var url='1〕http://localhost:8887/JAXRSService/webresources/generic/getAllUsers';
var用户id;
$.getJSON(url、函数(数据){
$.each(data.Actors,function(index,actor){
//缓存#nameSignIn和#passwordSignIn元素的值
var name=$('#nameSignIn').val();
var password=$('#passwordSignIn').val();
if(actor.first_name==name&&actor.password==password){
//我们找到了合适的演员。
//提取其ID并将其分配给userId。
userId=actor.id;
window.location.href=“content.html?userId=“+userId;
}
});
//只有在没有任何actor对象的情况下才应达到此警报
//具有与输入框值匹配的名称和密码。
警惕(“бббббббббббббббб107;
});
}
//在下一页。。。
//来自http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
//此方法可以处理具有多个查询参数的URL,
//您可能会在将来添加。
函数getQueryVariable(变量){
var query=window.location.search.substring(1);
var vars=query.split('&');
对于(变量i=0;i
阅读我的评论后,您可能想尝试以下方法:
var userId = 1;
function signInUser(){
$.getJSON('http://localhost:8887/JAXRSService/webresources/generic/getAllUsers', function(data){
var items = [], actors = data.Actors, l = 0;
$.each(actors, function(i, o){
l++;
if($('#nameSignIn').val() === o.first_name && $('#passwordSignIn').val() === o.password){
userId = o.id;
// this will redirect before any other code runs -> location = 'content.html';
if(l === actors.length){
alert('End of Loop');
}
}
});
});
}
signInUser();
我不会将敏感数据存储在JSON中,例如密码。使用数据库。也不需要同时获取所有数据。阅读我的评论后,您可能想尝试以下方法:
var userId = 1;
function signInUser(){
$.getJSON('http://localhost:8887/JAXRSService/webresources/generic/getAllUsers', function(data){
var items = [], actors = data.Actors, l = 0;
$.each(actors, function(i, o){
l++;
if($('#nameSignIn').val() === o.first_name && $('#passwordSignIn').val() === o.password){
userId = o.id;
// this will redirect before any other code runs -> location = 'content.html';
if(l === actors.length){
alert('End of Loop');
}
}
});
});
}
signInUser();
我不会将敏感数据存储在JSON中,例如密码。使用数据库。也不需要同时获取所有数据。感谢您的帮助。最后使用:
sessionStorage.getItem('label')
sessionStorage.setItem('label', 'value')
感谢您的帮助。最后使用:
sessionStorage.getItem('label')
sessionStorage.setItem('label', 'value')
为什么不将此信息存储在cookie中并根据需要检索/更新?为什么对数据运行双循环<代码>$。每个()
都可以。你的for-in循环是重复。如果data
是一个对象,那么它如何具有length
属性?请更好地缩进代码。{“Actors”:[{“first_name”:“Krasavchik”,“password”:“1234”},{“first_name”:“Kalach”,“password”:“first_name”:“Bandit”,“password”:“},{“first_name”:“TestName”,“password”:“},{“first_name”:“Oleg”,“password”:“Oleg”},{“firstĴ这是我的JSON响应。所以可能是这样。也许这不是必要的。但这不是真正的问题。方法i