topcool
topcool

Reputation: 2720

How to send parameter in ASP.NET Core 6 GET api Ajax call

I have an ASP.NET Core 6.0 Web API and want to call a GET method using ajax.

This is my Ajax call:

$.ajax({
   type: "GET",
   url: "/api/CountriesApi/GetById?id=" + id,
   success: function (result) {
         ...
      }
   }
});

And here is my action method in the API:

[HttpGet("{id}")]
public IActionResult GetById(int id)
{
  ...
}

My problem is that the parameter value is zero and I can't send id value to the action method.

Upvotes: 0

Views: 752

Answers (2)

Amit Kumar
Amit Kumar

Reputation: 9

Just remove equals sign from your url

Upvotes: 0

Kazi Rahiv
Kazi Rahiv

Reputation: 681

Make sure you pass proper integer value in your id parameter in the ajax call, Otherwise the .net code should work. If you are using default name from param you can opt-out the {id} also

[HttpGet]
public IActionResult GetById(int id)
{
  ...
}

Upvotes: 1

Related Questions