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

144
Views
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 answers
Answer question

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 Report

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 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!