Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
Unsupported media type ASP.NET Core Web API

On the front-end i use Angular to collect som data from the form and send it to my server-side controllers. As the image shows below, i get the data ($scope.newData) on my controller and service, but when it reaches the server, i get the following error: "Unsupported media type" and my newData is empty.

Website console

My controller:

// Create new salesman
  $scope.addSalesman = function (newData) {
    console.log("Controller");
    console.log($scope.newData);
    myService.addNewSalesman($scope.newData).then(function (data) {
      console.log(data);
    }, function (err) {
      console.log(err);
    });
  };

My service:

addNewSalesman: function (newData) {
            console.log("service");
            console.log(newData)
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url: '/api/Salesman',
                headers: { 'Content-type': 'application/json' }
            }, newData).then(function (res) {
                deferred.resolve(res.data);
            }, function (res) {
                deferred.reject(res);
            });
            return deferred.promise;
        }

My Salesman model:

public class Salesman
    {
        public int SalesmanID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string BirthDate { get; set; }
        public int Phone { get; set; }
        public string Adress { get; set; }
        public string City { get; set; }
        public int Postal { get; set; }
        public string Role { get; set; }
    }

My server-side controller:

[Route("api/[controller]")]
public class SalesmanController : Controller
{

    private readonly DataAccess _DataAccess;

    public SalesmanController() 
    { 
        _DataAccess = new DataAccess(); 
    } 

    [HttpPost]
    public IActionResult PostSalesman([FromBody] Salesman newData)
    {
        return Ok(newData); 
    }
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Just replace [FromBody] to [FromForm] in your controller.

about 4 years ago · Santiago Trujillo Denunciar

0

The header you are sending is wrong. You are sending Content-Type: application/json, but you have to send Accept: application/json.

Content-Type: application/json is what the server must send to the client and the client must send Accept to tell the server which type of response it accepts.

addNewSalesman: function (newData) {
        console.log("service");
        console.log(newData)
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: '/api/Salesman',
            headers: { 'Accept': 'application/json' }
        }, newData).then(function (res) {
            deferred.resolve(res.data);
        }, function (res) {
            deferred.reject(res);
        });
        return deferred.promise;
    }

Should do it. Also see "Content negotiation" on MDN.

about 4 years ago · Santiago Trujillo Denunciar

0

This is a CORS issue.

During development it's safe to accept all http request methods from all origins. Add the following to your startup.cs:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        //Accept All HTTP Request Methods from all origins
        app.UseCors(builder =>
            builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

        app.UseMvc();
    }

See here for more details about CORS.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda