Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
El .net core mvc está llegando al controlador json I POST, pero no puedo leerlo. También recibo el código de error 415 cuando uso [FromBody]

Estoy publicando datos con XHR en el proyecto que estoy codificando. Un JSON llega al controlador, pero si uso [FromBody] obtengo error code 415 . Si no lo uso, el JSON entrante parece vacío.

Controlador

 [HttpPost] [IgnoreAntiforgeryToken] [Route("revizedCategoryzfc")] public void revizedCategoryzfc([FromBody] Category model) { if (model.fileCode != null) { System.Console.WriteLine(model.fileCode); } System.Console.WriteLine("Girdi"); }

Publicar código XHR

 async function makeRequest(method, url, model) { return new Promise(function(resolve, reject) { let xhr = new XMLHttpRequest(); xhr.open(method, url); xhr.onload = function() { if (this.status >= 200 && this.status < 300) { resolve(xhr.response); }else { reject({ status: this.status, statusText: xhr.statusText }); } }; xhr.onerror = function() { reject({ status: this.status, statusText: xhr.statusText }); }; xhr.send(model); }); } document.querySelector('.save-service').addEventListener('click', async () => { for (var es of document.querySelectorAll('.category-item')) { await makeRequest('POST', '/zfc/revizedCategoryzfc', JSON.stringify({ "Name": es.querySelector('.service-name').value, "fileCode": es.querySelector('.filecodas').value, "CategoryId": es.querySelector('.zfc-id').value })) } })

Error

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Dado que usa json strngify y frombody, debe agregar este encabezado a su xhr

 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!