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

Time:08-25

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.

CodePudding user response:

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)
{
  ...
}

CodePudding user response:

Just remove equals sign from your url

  • Related