AlexeyS
✅ (solved) dot net cannot send post request to my endpoint
Hey guys, need some help understanding why my dotnet app is not working:
I have such js frontend that sends a post request to my 'localhost:8080/controller'
I have such coontroller in my dotnet app:
let btn = document.getElementById('submit');
let URL = 'localhost:8080/controller';
document.getElementById('result-value').value = false.toString();
btn.onclick = (e) => {
let req = new XMLHttpRequest();
req.open("POST", URL, true);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.onreadystatechange = function () {
if (req.readyState == XMLHttpRequest.DONE && req.status == 200) {
let answer = JSON.parse(req.response);
if (typeof (answer.access) != 'undefined') {
document.getElementById('result-value').value = answer.access.toString();
}
}
}
req.send(JSON.stringify({
Login: document.getElementById('login').value,
Password: document.getElementById('pass').value
}));
};
let btn = document.getElementById('submit');
let URL = 'localhost:8080/controller';
document.getElementById('result-value').value = false.toString();
btn.onclick = (e) => {
let req = new XMLHttpRequest();
req.open("POST", URL, true);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.onreadystatechange = function () {
if (req.readyState == XMLHttpRequest.DONE && req.status == 200) {
let answer = JSON.parse(req.response);
if (typeof (answer.access) != 'undefined') {
document.getElementById('result-value').value = answer.access.toString();
}
}
}
req.send(JSON.stringify({
Login: document.getElementById('login').value,
Password: document.getElementById('pass').value
}));
};
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace YourNamespace.Controllers
{
[ApiController]
[Route("[controller]")]
public class LoginController : ControllerBase
{
private static readonly Dictionary<string, string> Users = new Dictionary<string, string>
{
{"admin", "admin"},
{"dsfsdf", "sdfsdf"}
};
[HttpPost]
public IActionResult Post([FromBody] LoginRequest request)
{
if (Users.TryGetValue(request.Login, out var password) && password == request.Password)
{
return Ok(new { access = true });
}
return Ok(new { access = false });
}
}
public class LoginRequest
{
public string Login { get; set; }
public string Password { get; set; }
}
}
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace YourNamespace.Controllers
{
[ApiController]
[Route("[controller]")]
public class LoginController : ControllerBase
{
private static readonly Dictionary<string, string> Users = new Dictionary<string, string>
{
{"admin", "admin"},
{"dsfsdf", "sdfsdf"}
};
[HttpPost]
public IActionResult Post([FromBody] LoginRequest request)
{
if (Users.TryGetValue(request.Login, out var password) && password == request.Password)
{
return Ok(new { access = true });
}
return Ok(new { access = false });
}
}
public class LoginRequest
{
public string Login { get; set; }
public string Password { get; set; }
}
}
119 replies