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

145
Vistas
Web API 2 looks for get when using ajax post?

I have this AJAX call:

 let errorModel = {
            exception: exception,
            extraMessage: extraMessage,
            time: Date.now()
        }
            $.ajax({
            url: "/api/error/LogError",
            type: "POST",
            data: errorModel
        });

And this web api 2 method here in this controller

 [RoutePrefix("api/Error")]
public class ErrorController : ApiController
{

[HttpPost]
    [Route("LogError")]
    public IHttpActionResult LogError([FromBody]ErrorModel errorModel)
    {

        try
        {
            _db.ErrorModels.Add(errorModel);
            _db.SaveChanges();
            return Created(new Uri(Request.RequestUri + "/" + errorModel.Id), errorModel);

        }
        catch (Exception e)
        {
            return BadRequest(e.Message);
        }
        }
}

But when my code reaches the ajax call, I get an error message like this:

{
  "message": "The requested resource does not support http method 'GET'."
}

What am I doing wrong here? I have plenty of api calls like this, and all of them work.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I have tested this code and found it working fine.

The error message you posted seems indicate that you are using 'get' method in ajax instead of 'post' method.

I have found the result from popular REST-client 'postman'.

Note: I have used string data-type for all of the properties of ErrorModel

enter image description here

about 4 years ago · Santiago Trujillo Denunciar

0

Hi all sorry for taking your time it seems it was a stupid error on my side, it seems my javascript Date.Now() wasn't returning what i expected and the api method received it as 00/00/0001 which was denied by the SQL server. Am not sure why the error message was saying what it was but after i fixed that by setting datetime in the api call the error resolved it self. Thanks all for your time

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