web api不显示数据 [英] web api is not displaying data

查看:217
本文介绍了web api不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的webapi控制器

This is my webapi controller

public class CategoryController : ApiController
    {
        List<category> category;

        public class DataResult
        {
            public List<category> Category { get; set; }
        }

        #region Categories
        public DataResult GetCategories()
        {
            DataResult result = new DataResult();
            
            try
            {
                using (MyDB db = new MyDB())
                {
                    category = db.Category.OrderBy(c => c.Name).ToList();

                }
                if (category == null)
                {
                    throw new HttpResponseException(
                           Request.CreateResponse(HttpStatusCode.NotFound));
                }
                else
                {
                    result.Category = category;
                   
                    return result;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }



这是我的jquery调用document.ready()




This is my jquery calling in document.ready()

$.ajax({
       url: "/api/Category",
       type: "GET",
       dataType: 'json',
       success: function (data) {
           alert('hai');
       },
       error: function (err) {
           document.write(data);
       }
   });



从entityframework datacontext访问数据,但它没有显示alert.if传递静态数据然后它正在工作。



这里/ api /是foldername,类别是Api控制器。



调用控制器方法,但警报没有提高,为什么?


am accessing data from entityframework datacontext,but it is not displaying alert.if am passing static data then it is working.

here /api/ is foldername,Category is Api Controller.

It is calling the controller method,but alert is not raising,why?

推荐答案

.ajax({
url: / api / Category
类型: GET
dataType:' json'
成功:函数(数据) {
alert(' hai');
},
错误:函数(错误){
document.write(data);
}
});
.ajax({ url: "/api/Category", type: "GET", dataType: 'json', success: function (data) { alert('hai'); }, error: function (err) { document.write(data); } });



从entityframework datacontext访问数据,但它没有显示alert.if传递静态数据然后它正在工作。



这里/ api /是foldername,类别是Api控制器。



调用控制器方法,但警报没有提高,为什么?


am accessing data from entityframework datacontext,but it is not displaying alert.if am passing static data then it is working.

here /api/ is foldername,Category is Api Controller.

It is calling the controller method,but alert is not raising,why?


您是否介入了代码?有数据要返回吗?如果返回错误,则应显示实际错误。实际上,您应该使用调试器在编译器中查看它是什么。它可能是各种各样的东西(一个破碎的EF模型就是其中之一)。为什么您的方法之外的类别列表?什么是MyDB?这是你的databasecontext?我将你的数据层包装起来比...更好...



你需要先做一些调试,然后如果你不能解决它,给我们可以使用一些信息。
Have you stepped through the code ? Is there data to return ? If there is an error being returned, then you should show what the actual error is. In fact, you should use your debugger to see what it is, inside the compiler. It could be all sorts of things ( a broken EF model is one ). Why is your list of categories outside the method ? What is MyDB ? It's your databasecontext ? I'd wrap your data layer up a bit nicer than that...

You need to do some debugging first, then if you can't solve it, give us some info to work with.


在服务器端,Json是一个字符串。如何将DataResult类转换为Json String?
On server side Json is a string. How do you transfer DataResult Class into Json String?


这篇关于web api不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆