C
C#ā€¢12mo 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
AlexeySā€¢12mo 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ā€¢12mo ago
Your url is wrong
AlexeyS
AlexeySā€¢12mo ago
in js file?
ero
eroā€¢12mo ago
Your controller sits at /login Yes
AlexeyS
AlexeySā€¢12mo ago
I thought: [Route("[controller]")] means /controller
ero
eroā€¢12mo ago
No, that would be [Route("controller")] The brackets format the class name
AlexeyS
AlexeySā€¢12mo ago
gotcha well, changed it and still doesn't work
AlexeyS
AlexeySā€¢12mo ago
AlexeyS
AlexeySā€¢12mo ago
let URL = 'localhost:8080/login';
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo ago
hmm
AlexeyS
AlexeySā€¢12mo ago
well it appears to be working if I send it by curl
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo ago
so what's the fastest way to "host" my html file?
Pobiega
Pobiegaā€¢12mo ago
are you using VS Code?
AlexeyS
AlexeySā€¢12mo ago
yup
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo ago
basically I have this
Pobiega
Pobiegaā€¢12mo ago
this get this extension
AlexeyS
AlexeySā€¢12mo ago
AlexeyS
AlexeySā€¢12mo ago
hosted at 127.0.0.1/5500
Pobiega
Pobiegaā€¢12mo ago
good now, fix the CORS issue in your program.cs
AlexeyS
AlexeySā€¢12mo 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ā€¢12mo 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
AlexeySā€¢12mo 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ā€¢12mo ago
well yes it needs to go before any other app.Use middleware order matters
AlexeyS
AlexeySā€¢12mo 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ā€¢12mo ago
looks better.
AlexeyS
AlexeySā€¢12mo ago
just right after app declaration
AlexeyS
AlexeySā€¢12mo ago
AlexeyS
AlexeySā€¢12mo 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ā€¢12mo ago
thats a different middleware, probably not related. okay, lets re-order some middleware and add another
AlexeyS
AlexeySā€¢12mo ago
I'm ready
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo 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ā€¢12mo ago
uhm you removed all the middleware after cors, including your controller mapping
AlexeyS
AlexeySā€¢12mo ago
true
Pobiega
Pobiegaā€¢12mo ago
dont do that :p re add everything after cors, except HttpsRedirection
AlexeyS
AlexeySā€¢12mo 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ā€¢12mo ago
looks better give it a spin
AlexeyS
AlexeySā€¢12mo ago
still nope :(
Pobiega
Pobiegaā€¢12mo ago
hang on, creating a new project to test
AlexeyS
AlexeySā€¢12mo ago
AlexeyS
AlexeySā€¢12mo ago
by the way
Pobiega
Pobiegaā€¢12mo ago
yeah, CORS errors. but we already knew that can you pass me your html?
AlexeyS
AlexeySā€¢12mo ago
sure
MODiX
MODiXā€¢12mo ago
Please don't upload any potentially harmful files @AlexeyS, your message has been removed
AlexeyS
AlexeySā€¢12mo 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ā€¢12mo ago
nah thats not it the cors is the issue for sure, but I cant seem to reproduce for some reason
AlexeyS
AlexeySā€¢12mo ago
well yeah maybe something wrong with my pc after all idk
Pobiega
Pobiegaā€¢12mo ago
I got it working when I swapped to fetch instead of using XmlRequest
AlexeyS
AlexeySā€¢12mo ago
hmm unfortunately I cannot change any front end code only backend
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo ago
trying
Pobiega
Pobiegaā€¢12mo ago
using .net 7 btw
AlexeyS
AlexeySā€¢12mo ago
me too hmm wait
AlexeyS
AlexeySā€¢12mo ago
still :(
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo ago
yess it works thanks man stupid error
Pobiega
Pobiegaā€¢12mo ago
thats what we get for not reading the error message properly šŸ™‚
AlexeyS
AlexeySā€¢12mo ago
yup and it changes here to true
AlexeyS
AlexeySā€¢12mo ago
yeah thanks for help man, really appreciate it
Pobiega
Pobiegaā€¢12mo ago
I would highly suggest using fetch and async instead of XmlHttpRequest thou
AlexeyS
AlexeySā€¢12mo ago
yeah but the code is not mine, that was a, let's say an "exercise"
Pobiega
Pobiegaā€¢12mo 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
AlexeySā€¢12mo 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ā€¢12mo ago
Was about to say, why would anybody use XmlHttpRequest nowadays lol
SinFluxx
SinFluxxā€¢12mo ago
$close
MODiX
MODiXā€¢12mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server
More Posts
Need help with finding one option in VSHello I'm learning with one course and they want me to download one tool but i don't have the Get Toā” Can't access Microsoft Graph. What part am I not understanding clearly?I will give my steps. The code. and the problem. Step 1. Login using Swagger UI [See Images] Step 2.ā” VSCode shows type definition and not the actually implementationWhen I press F12 or use the `Go To Definition` option, it only shows me the definition without any iā” Attempting to create a Result<T, E> type, running into issues with subclassesI want to create a `Result<T, E>` and I'm running into trouble with the following: let's say I have ā” Help with college C++ lab (need help ASAP, fell behind due to medical and work reasons)I believe I have 90% of this lab done correctly but I am held up on seemingly small issues that I amāœ… C sharp help static voidHello everyone I am new to C sharp and I was wondering if someone can help me with my code it says tā” Form in ASP.NET.MVC doesn't workhi, guys! nice to meet y'all, I just got here! :) this may sound like a silly question, but I'm newā” Just trying to use Accord.Video.FFMPEG in C# projectSorry if my question is super stupid, literally 1 day old in C# (I have prog. exp. in other langs tgetting warning: CS8618 - Non-nullable variable must contain a non-null value when exiting constructI guess, it's not a big deal since the code is getting compiled but I still want to understand why Iā” Need help with user accountI'm creating user account using methods, but it seems a bit unclear to me how to invoke and use thes