I need to call a web api, this web api method is accepting two parameter one is enum type and another is int. I need to call this api using Ajax Jquery. How to pass enum paramter.
API Code
[HttpGet]
public HttpResponseMessage GetActivitiesByType(UnifiedActivityType type, int pageNumber)
{
var activities = _activityService.GetAllByType(type, pageNumber);
return Request.CreateResponse(HttpStatusCode.OK, activities);
}
My Ajax Call Code:
var jsonData = { 'type': 'Recommendation', pageNumber: 0 };
$.ajax({
type: 'get',
dataType: 'json',
crossDomain: true,
url: CharismaSystem.globaldata.RemoteURL_ActivityGetAllByType,
contentType: "application/json; charset=utf-8",
data: jsonData,
headers: { "authorization": token },
error: function (xhr) {
alert(xhr.responseText);
},
success: function (data) {
alert('scuess');
}
});
Any Suggestions..
An enum is treated as a 0-based integer, so change:
var jsonData = { 'type': 'Recommendation', pageNumber: 0 };
To this:
var jsonData = { 'type': 0, pageNumber: 0 };
The client needs to know the index numbers. You could hard code them, or send them down in the page using @Html.Raw(...), or make them available in another ajax call.
You can not pass enum
from client side. Either pass string
or int
and on the server side cast that value to enum
. Something like,
MessagingProperties result;
string none = "None";
result = (MessagingProperties)Enum.Parse(typeof(MessagingProperties), none);
Or you can do something similar to this post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With