Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Is it working now? –  gmail user Jan 20 at 20:27

2 Answers 2

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

share|improve this answer

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.

share|improve this answer
    
It didn't worked for me it gave me error "500 Critical Exception". –  user3215002 Jan 20 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.