I have been working on an application using asp net core with Razor Pages. I spent a lot of time trying to figure out how to process POST requests (specifically OnPost in razor pages). After reading through the documentation at "https://learn.microsoft.com/ru-ru/aspnet/core/security/anti-request-forgery?view=aspnetcore-8.0", I decided to add a secure token for JavaScript and started processing POST requests. However, now I am getting a Fetch: TypeError error in response from the server. It seems like I'm missing something important, but I can't quite figure out what it is. When I start processing the request with "app.Map", everything works fine without any issues.
Issue 1: Firefox shows - Content-Length header of network response exceeds response Body. How can I resolve this issue in my code?
Issue 2: It's interesting that the headers are the same and do not contain a Content-Length when I use app.Map it works fine, but if I use a razor page, I encounter an exception.
Headers: { "content-type" - "application/json; charset=utf-8", date - "Mon, 08 Jul 2024 04:22:46 GMT", server - "Kestrel", "x-firefox-spdy" - "h2" }
This is part of my working code:
app.Map("/FormResult/GetRes", HandleMapFormRes);
static void HandleMapFormRes(IApplicationBuilder app)
{
string text = string.Empty;
app.Run(async context =>
{
if (context.Request.Method == "POST")
{
try
{
/* .......
.......
.......
....... */
JsonFormToClient jsonFormToClient = new JsonFormToClient();
jsonFormToClient.Blockinfo = results;
jsonFormToClient.CountOfBlocks = c;
string ResponceJson = JsonConvert.SerializeObject(jsonFormToClient);
await context.Response.WriteAsJsonAsync(ResponceJson);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
});
}
function drawChart(pathToPage,token) {
var htmlContent = pathToPage;
var formData = new FormData();
formData.append('htmlContent', htmlContent);
fetch(pathToPage, {
method: 'POST',
body: formData,
headers: {
"X-XSRF-TOKEN": token
},
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
var a = response.json();
return a;
})
.then(jsonData => {
a = JSON.parse(jsonData);
for (let i = 0; i < a.CountOfBlocks; i++) {
AddNewBlock(a.Blockinfo[i].Id_in_html);
drawnow(a.Blockinfo[i]);
}
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
If I process this with Razor Page (OnPost), the response will break