C
C#11mo ago
Whiteboy

✅ ASP.NET Fetch request to MySQL

So i want to chcek if typed login exisits in db but i get all time error 404
Error checking login existence: Error: HTTP error! Status: 404
at 3:176:31
(anonymous) @ 3:191
Promise.catch (async)
checkLoginExists @ 3:190
checkLogins @ 3:202
(anonymous) @ 3:208
Error checking login existence: Error: HTTP error! Status: 404
at 3:176:31
(anonymous) @ 3:191
Promise.catch (async)
checkLoginExists @ 3:190
checkLogins @ 3:202
(anonymous) @ 3:208
function checkLoginExists(login) {
fetch(`/AssignTask/{taskId}?handler=CheckLoginExists&login=${login}`, { method: 'GET' })
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
const loginErrorMessage = document.getElementById('loginErrorMessage');

if (!data.exists) {
loginErrorMessage.textContent = `User with login '${login}' not found in the database.`;
} else {
loginErrorMessage.textContent = ''; // Clear the error message
enableAssignButton();
}
})
.catch(error => {
console.error('Error checking login existence:', error);
// Handle the error, e.g., display a generic error message
});
}

function checkLogins(logins) {
let loginLines = logins.split("\n");

for (let i = 0; i < loginLines.length; i++) {
let login = loginLines[i].trim();
if (login !== "") {
checkLoginExists(login);
}
}
}
function checkLoginExists(login) {
fetch(`/AssignTask/{taskId}?handler=CheckLoginExists&login=${login}`, { method: 'GET' })
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
const loginErrorMessage = document.getElementById('loginErrorMessage');

if (!data.exists) {
loginErrorMessage.textContent = `User with login '${login}' not found in the database.`;
} else {
loginErrorMessage.textContent = ''; // Clear the error message
enableAssignButton();
}
})
.catch(error => {
console.error('Error checking login existence:', error);
// Handle the error, e.g., display a generic error message
});
}

function checkLogins(logins) {
let loginLines = logins.split("\n");

for (let i = 0; i < loginLines.length; i++) {
let login = loginLines[i].trim();
if (login !== "") {
checkLoginExists(login);
}
}
}
11 Replies
Whiteboy
WhiteboyOP11mo ago
public JsonResult OnGetCheckLogin(string login)
{
return dbContext.Users.Any(user => user.Login == login)
? new JsonResult(new { exists = true })
: new JsonResult(new { exists = false });
}
public JsonResult OnGetCheckLogin(string login)
{
return dbContext.Users.Any(user => user.Login == login)
? new JsonResult(new { exists = true })
: new JsonResult(new { exists = false });
}
The page is also created from taskId like this
@page "/AssignTask/{taskId:int}"
@page "/AssignTask/{taskId:int}"
so idk if i added it correctly to the fetch request
Angius
Angius11mo ago
Ah, callback hell, haven't seen it in a while lol
Whiteboy
WhiteboyOP11mo ago
i made same for task creation t ocheck if title is unique and it works fine tho
Angius
Angius11mo ago
The OnCheckLogin() is a handler in the Razor page?
Whiteboy
WhiteboyOP11mo ago
yes
Angius
Angius11mo ago
No description
Angius
Angius11mo ago
No description
Angius
Angius11mo ago
There is no CheckLoginExists handler
Whiteboy
WhiteboyOP11mo ago
public JsonResult OnGetCheckLoginExists(string login)
{
return dbContext.Users.Any(user => user.Login == login)
? new JsonResult(new { exists = true })
: new JsonResult(new { exists = false });
}
public JsonResult OnGetCheckLoginExists(string login)
{
return dbContext.Users.Any(user => user.Login == login)
? new JsonResult(new { exists = true })
: new JsonResult(new { exists = false });
}
like this? didn't fix the issue tho
Angius
Angius11mo ago
Huh I'm gonna be honest, I never used named handlers, let alone as some sort of a pseudo-API I just always had my API endpoints separate
Whiteboy
WhiteboyOP11mo ago
td; dr; to fix it get pageID (taskId)
let taskId = @taskId;
fetch(`/AssignTask/${taskId}?handler=CheckLoginExists&login=${login}`, { method: 'GET' })
let taskId = @taskId;
fetch(`/AssignTask/${taskId}?handler=CheckLoginExists&login=${login}`, { method: 'GET' })
Want results from more Discord servers?
Add your server