Я создаю простую страницу входа в систему, используя JS, которая получает пользовательские данные из https://jsonplaceholder.typicode.com/users.
Я пытаюсь проверить, существует ли введенное имя пользователя или электронная почта, но Fetch () всегда возвращает пустой результат или не определен.const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
const found = users.find(u => u.username === input || u.email === input);
< /code>
Simple Login
body {
font-family: sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background: #eee;
}
.box {
background: #fff;
padding: 25px 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input {
padding: 10px;
width: 220px;
font-size: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 16px;
font-size: 15px;
margin-left: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
#message {
color: red;
margin-top: 15px;
}
#result {
color: green;
margin-top: 10px;
}
Log In
async function login() {
const input = document.getElementById('userInput');
const value = input.value.trim().toLowerCase();
const message = document.getElementById('message');
const result = document.getElementById('result');
message.textContent = '';
result.textContent = '';
if (value.length < 3) {
message.textContent = 'Too short. Min 3 characters.';
return;
}
try {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
const found = users.find(u =>
u.username.toLowerCase() === value || u.email.toLowerCase() === value
);
if (found) {
localStorage.setItem('user', JSON.stringify(found));
result.textContent = `hello ${found.username.toLowerCase()}...`;
} else {
message.textContent = "We don;t have this USerd";
}
} catch (err) {
message.textContent = 'Could not connect.';
console.log(err);
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... in-checker
Почему Fetch всегда возвращает пустое или неопределенное в моем шахте входа в систему? [закрыто] ⇐ Html
Программисты Html
1751437276
Anonymous
Я создаю простую страницу входа в систему, используя JS, которая получает пользовательские данные из https://jsonplaceholder.typicode.com/users.
Я пытаюсь проверить, существует ли введенное имя пользователя или электронная почта, но Fetch () всегда возвращает пустой результат или не определен.const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
const found = users.find(u => u.username === input || u.email === input);
< /code>
Simple Login
body {
font-family: sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background: #eee;
}
.box {
background: #fff;
padding: 25px 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
input {
padding: 10px;
width: 220px;
font-size: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 16px;
font-size: 15px;
margin-left: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
#message {
color: red;
margin-top: 15px;
}
#result {
color: green;
margin-top: 10px;
}
Log In
async function login() {
const input = document.getElementById('userInput');
const value = input.value.trim().toLowerCase();
const message = document.getElementById('message');
const result = document.getElementById('result');
message.textContent = '';
result.textContent = '';
if (value.length < 3) {
message.textContent = 'Too short. Min 3 characters.';
return;
}
try {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
const found = users.find(u =>
u.username.toLowerCase() === value || u.email.toLowerCase() === value
);
if (found) {
localStorage.setItem('user', JSON.stringify(found));
result.textContent = `hello ${found.username.toLowerCase()}...`;
} else {
message.textContent = "We don;t have this USerd";
}
} catch (err) {
message.textContent = 'Could not connect.';
console.log(err);
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79686612/why-does-fetch-always-return-empty-or-undefined-in-my-login-checker[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия