阿贾克斯jQuery的请求通过Web API
我试图找回从Web API调用JSON结果。
I'm trying to retrieve a JSON result from a WEB API call.
我的WEP API方法:
My WEP API method:
[AcceptVerbs("GET", "POST")]
public object GetTest()
{
rep = new ChatRepository();
chatBoxCLS box = rep.Chatrequest(chatRequestLevel.Parent, null);
System.Web.Mvc.JsonResult jsonResult = new System.Web.Mvc.JsonResult
{
Data = box,
JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
};
return jsonResult.Data;
}
我已修改WebapiConfig.cs如下,使得它总是返回JSON
I have modified the WebapiConfig.cs as below, so that it will always return JSON
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
下面是我的Jquery Ajax调用:
Below is my Jquery ajax call:
<script type='text/javascript'>
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'http://localhost:6606/api/values/GetTest',
dataType: 'json',
crossDomain: true,
success: function (msg) {
alert('success');
},
error: function (request, status, error) {
alert('error');
}
});
});
</script>
它总是在错误警报结束。没有数据从WEB API接收。我试着调试,发现我的要求成功命中WEB API方法并返回JSON。下面是它返回的JSON数据。
It's always end up in error alert. no data is received from the WEB API. I tried debugging and found that my request successfully hits the WEB API method and return JSON. Below is the JSON data it returns.
{LISTOFITEMS:[{身份证:14,说明:新
test\",\"display_number\":1},{\"id\":4,\"description\":\"operational\",\"display_number\":2},{\"id\":3,\"description\":\"sales\",\"display_number\":3},{\"id\":5,\"description\":\"technical\",\"display_number\":4}],\"reply\":null,\"history\":null,\"Initialhistory\":null,\"Question\":\"\",\"chatids\":null,\"displayNum\":null}
{"listOfItems":[{"id":14,"description":"New test","display_number":1},{"id":4,"description":"operational","display_number":2},{"id":3,"description":"sales","display_number":3},{"id":5,"description":"technical","display_number":4}],"reply":null,"history":null,"Initialhistory":null,"Question":"","chatids":null,"displayNum":null}
为什么我不能在客户端获得任何结果呢?
why am i not getting any result on client side?
我通过添加访问控制允许来源以响应头解决了这个问题。
I solved the issue by adding Access-Control-Allow-Origin to response header
public class CrossDomainActionFilter : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
bool needCrossDomain = true;
if (needCrossDomain)
{
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
}
base.OnActionExecuted(actionExecutedContext);
}
}
[AcceptVerbs("GET", "POST")]
[CrossDomainActionFilter]
public object GetTest()
{
rep = new ChatRepository();
chatBoxCLS box = rep.Chatrequest(chatRequestLevel.Parent, null);
System.Web.Mvc.JsonResult jsonResult = new System.Web.Mvc.JsonResult
{
Data = box,
JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
};
return jsonResult.Data;
}