use*_*002 8 asp.net-mvc jquery enums
我需要调用一个web api,这个web api方法接受两个参数,一个是枚举类型,另一个是int.我需要使用Ajax Jquery来调用这个api.如何通过枚举参数.
API代码
[HttpGet]
public HttpResponseMessage GetActivitiesByType(UnifiedActivityType type, int pageNumber)
{
var activities = _activityService.GetAllByType(type, pageNumber);
return Request.CreateResponse(HttpStatusCode.OK, activities);
}
Run Code Online (Sandbox Code Playgroud)
我的Ajax电话代码:
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');
}
});
Run Code Online (Sandbox Code Playgroud)
有什么建议..
枚举被视为基于0的整数,因此更改:
var jsonData = { 'type': 'Recommendation', pageNumber: 0 };
Run Code Online (Sandbox Code Playgroud)
对此:
var jsonData = { 'type': 0, pageNumber: 0 };
Run Code Online (Sandbox Code Playgroud)
客户需要知道索引号.您可以对它们进行硬编码,或者使用@ Html.Raw(...)将它们发送到页面中,或者在另一个ajax调用中使它们可用.