Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda