C
C#ā€¢17mo ago
AlexeyS

āœ… (solved) dot net cannot send post request to my endpoint

Hey guys, need some help understanding why my dotnet app is not working:
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
}));
};
I have such js frontend that sends a post request to my 'localhost:8080/controller' I have such coontroller in my dotnet app:
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; }
}
}
69 Replies
AlexeyS
AlexeySOPā€¢17mo ago
here is my program.cs file:
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run("http://localhost:8080");
ero
eroā€¢17mo ago
Your url is wrong
AlexeyS
AlexeySOPā€¢17mo ago
in js file?
ero
eroā€¢17mo ago
Your controller sits at /login Yes
AlexeyS
AlexeySOPā€¢17mo ago
I thought: [Route("[controller]")] means /controller
ero
eroā€¢17mo ago
No, that would be [Route("controller")] The brackets format the class name
AlexeyS
AlexeySOPā€¢17mo ago
gotcha well, changed it and still doesn't work
AlexeyS
AlexeySOPā€¢17mo ago
AlexeyS
AlexeySOPā€¢17mo ago
let URL = 'localhost:8080/login';
Pobiega
Pobiegaā€¢17mo ago
not sure you can issue requests like that from a file:// use live-server or similar to run your frontend so it uses the http protocol you're getting a CORS error because your frontend and backend have different origins atm so you'll also need to disable CORS in .NET
AlexeyS
AlexeySOPā€¢17mo ago
hmm
AlexeyS
AlexeySOPā€¢17mo ago
well it appears to be working if I send it by curl
Pobiega
Pobiegaā€¢17mo ago
well yes, since CURL uses the http protocol šŸ™‚ unlike file:// also, CURL issues a raw http request. its not a browser, so there is no CORS going on. CORS is a client-side thing built into browsers to prevent redirect attacks
AlexeyS
AlexeySOPā€¢17mo ago
so what's the fastest way to "host" my html file?
Pobiega
Pobiegaā€¢17mo ago
are you using VS Code?
AlexeyS
AlexeySOPā€¢17mo ago
yup
Pobiega
Pobiegaā€¢17mo ago
Name: Live Server Id: ritwickdey.LiveServer Description: Launch a development local Server with live reload feature for static & dynamic pages Version: 5.7.9 Publisher: Ritwick Dey VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer
Live Server - Visual Studio Marketplace
Extension for Visual Studio Code - Launch a development local Server with live reload feature for static & dynamic pages
AlexeyS
AlexeySOPā€¢17mo ago
basically I have this
Pobiega
Pobiegaā€¢17mo ago
this get this extension
AlexeyS
AlexeySOPā€¢17mo ago
AlexeyS
AlexeySOPā€¢17mo ago
hosted at 127.0.0.1/5500
Pobiega
Pobiegaā€¢17mo ago
good now, fix the CORS issue in your program.cs
AlexeyS
AlexeySOPā€¢17mo ago
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run("http://localhost:8080");
I've never used c# before in my life
Pobiega
Pobiegaā€¢17mo ago
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});
this will "disable" cors by allowing everything
AlexeyS
AlexeySOPā€¢17mo ago
well
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

app.Run("http://localhost:8080");
I put it like that still getting cors errors
Pobiega
Pobiegaā€¢17mo ago
well yes it needs to go before any other app.Use middleware order matters
AlexeyS
AlexeySOPā€¢17mo ago
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();


app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();


app.Run("http://localhost:8080");
Pobiega
Pobiegaā€¢17mo ago
looks better.
AlexeyS
AlexeySOPā€¢17mo ago
just right after app declaration
AlexeyS
AlexeySOPā€¢17mo ago
AlexeyS
AlexeySOPā€¢17mo ago
just sent a post request via curl:
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
logged this
Pobiega
Pobiegaā€¢17mo ago
thats a different middleware, probably not related. okay, lets re-order some middleware and add another
AlexeyS
AlexeySOPā€¢17mo ago
I'm ready
Pobiega
Pobiegaā€¢17mo ago
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});
AlexeyS
AlexeySOPā€¢17mo ago
what parts of code should I delete to put this
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

app.Run("http://localhost:8080");
like this?
Pobiega
Pobiegaā€¢17mo ago
uhm you removed all the middleware after cors, including your controller mapping
AlexeyS
AlexeySOPā€¢17mo ago
true
Pobiega
Pobiegaā€¢17mo ago
dont do that :p re add everything after cors, except HttpsRedirection
AlexeyS
AlexeySOPā€¢17mo ago
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}


app.UseAuthorization();

app.MapControllers();


app.Run("http://localhost:8080");
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(corsPolicyBuilder =>
{
corsPolicyBuilder.AllowAnyOrigin();
corsPolicyBuilder.AllowAnyMethod();
corsPolicyBuilder.AllowAnyHeader();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}


app.UseAuthorization();

app.MapControllers();


app.Run("http://localhost:8080");
Pobiega
Pobiegaā€¢17mo ago
looks better give it a spin
AlexeyS
AlexeySOPā€¢17mo ago
still nope :(
Pobiega
Pobiegaā€¢17mo ago
hang on, creating a new project to test
AlexeyS
AlexeySOPā€¢17mo ago
AlexeyS
AlexeySOPā€¢17mo ago
by the way
Pobiega
Pobiegaā€¢17mo ago
yeah, CORS errors. but we already knew that can you pass me your html?
AlexeyS
AlexeySOPā€¢17mo ago
sure
MODiX
MODiXā€¢17mo ago
Please don't upload any potentially harmful files @AlexeyS, your message has been removed
AlexeyS
AlexeySOPā€¢17mo ago
let btn = document.getElementById('submit');
let URL = 'localhost:8080/login';
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/login';
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
}));
};
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<link href="main.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<input id="login" type="text" placeholder="enter ur name" />
<input id="pass" type="password" placeholder="enter ur password" />
<button id="submit">Log In</button>
<div id="result">
<input id="result-value" type="text" disabled="disabled"/>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<link href="main.css" rel="stylesheet" />
</head>
<body>
<div class="wrapper">
<input id="login" type="text" placeholder="enter ur name" />
<input id="pass" type="password" placeholder="enter ur password" />
<button id="submit">Log In</button>
<div id="result">
<input id="result-value" type="text" disabled="disabled"/>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
body {
margin: 0px;
padding:0px;
box-sizing:border-box;
user-select:none;
}

.wrapper {
margin: 200px auto 0 auto;
padding: 30px;
border: 2px solid #808080;
border-radius: 5px;
width: 50%;
}

.wrapper input {
border: 1px solid #808080;
border-radius: 3px;
font-size: 16px;
letter-spacing: 2px;
background-color: #dcdcdc;
width: 80%;
margin: 50px auto;
padding: 5px;
outline: none;
}

#submit {
display: block;
width: 100px;
cursor: pointer;
background-color: #9bffa6;
border: none;
outline: none;
padding: 10px;
border-radius: 5px;
}

#submit:hover {
transition: .5s all ease;
background-color:#67fa3d;
}

#result {
width:120px;
background-color:#97f5ef;
padding:10px 20px;
margin:50px auto;
text-align:center;
border-radius:5px;
}

#result input{
width:100%;
height:100%;
margin:0;
padding:0;
background-color:transparent;
border:none;
outline:none;
user-select:none;
text-align:center;
}
body {
margin: 0px;
padding:0px;
box-sizing:border-box;
user-select:none;
}

.wrapper {
margin: 200px auto 0 auto;
padding: 30px;
border: 2px solid #808080;
border-radius: 5px;
width: 50%;
}

.wrapper input {
border: 1px solid #808080;
border-radius: 3px;
font-size: 16px;
letter-spacing: 2px;
background-color: #dcdcdc;
width: 80%;
margin: 50px auto;
padding: 5px;
outline: none;
}

#submit {
display: block;
width: 100px;
cursor: pointer;
background-color: #9bffa6;
border: none;
outline: none;
padding: 10px;
border-radius: 5px;
}

#submit:hover {
transition: .5s all ease;
background-color:#67fa3d;
}

#result {
width:120px;
background-color:#97f5ef;
padding:10px 20px;
margin:50px auto;
text-align:center;
border-radius:5px;
}

#result input{
width:100%;
height:100%;
margin:0;
padding:0;
background-color:transparent;
border:none;
outline:none;
user-select:none;
text-align:center;
}
maybe something is wrong with my controller:
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; }
}
}
Pobiega
Pobiegaā€¢17mo ago
nah thats not it the cors is the issue for sure, but I cant seem to reproduce for some reason
AlexeyS
AlexeySOPā€¢17mo ago
well yeah maybe something wrong with my pc after all idk
Pobiega
Pobiegaā€¢17mo ago
I got it working when I swapped to fetch instead of using XmlRequest
AlexeyS
AlexeySOPā€¢17mo ago
hmm unfortunately I cannot change any front end code only backend
Pobiega
Pobiegaā€¢17mo ago
okay.
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();

app.UseCors(x =>
{
x.AllowAnyHeader();
x.AllowAnyMethod();
x.AllowAnyOrigin();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseRouting();

app.UseCors(x =>
{
x.AllowAnyHeader();
x.AllowAnyMethod();
x.AllowAnyOrigin();
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

app.Run();
thats my program.cs it works, no CORS errors
AlexeyS
AlexeySOPā€¢17mo ago
trying
Pobiega
Pobiegaā€¢17mo ago
using .net 7 btw
AlexeyS
AlexeySOPā€¢17mo ago
me too hmm wait
AlexeyS
AlexeySOPā€¢17mo ago
still :(
Pobiega
Pobiegaā€¢17mo ago
read the top error ah I know the issue your URL let URL = "https://localhost:7242/login"; you didnt specify a protocl in your URL
AlexeyS
AlexeySOPā€¢17mo ago
yess it works thanks man stupid error
Pobiega
Pobiegaā€¢17mo ago
thats what we get for not reading the error message properly šŸ™‚
AlexeyS
AlexeySOPā€¢17mo ago
yup and it changes here to true
AlexeyS
AlexeySOPā€¢17mo ago
yeah thanks for help man, really appreciate it
Pobiega
Pobiegaā€¢17mo ago
I would highly suggest using fetch and async instead of XmlHttpRequest thou
AlexeyS
AlexeySOPā€¢17mo ago
yeah but the code is not mine, that was a, let's say an "exercise"
Pobiega
Pobiegaā€¢17mo ago
right
btn.onclick = async (e) => {
const response = await fetch(URL, { method: "POST" });
const content = await response.json();
resval.value = content.access;
};
btn.onclick = async (e) => {
const response = await fetch(URL, { method: "POST" });
const content = await response.json();
resval.value = content.access;
};
like, how much nicer is that šŸ˜„
AlexeyS
AlexeySOPā€¢17mo ago
decided to take a part in hackaton and they haven't even mentioned anywhere that you would need to write code in c# yeah looks way better
Angius
Angiusā€¢17mo ago
Was about to say, why would anybody use XmlHttpRequest nowadays lol
SinFluxx
SinFluxxā€¢17mo ago
$close
MODiX
MODiXā€¢17mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server