Я создаю простую страницу входа в систему, используя 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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Почему Fetch всегда возвращает пустое или неопределенное в моем шахте входа в систему?
Anonymous » » в форуме Html - 0 Ответы
- 4 Просмотры
-
Последнее сообщение Anonymous
-
-
-
JavaScript Fetch Body пустое при использовании CloudFlare [закрыто]
Anonymous » » в форуме Javascript - 0 Ответы
- 8 Просмотры
-
Последнее сообщение Anonymous
-