无法在HTML页面中使用MVC4 Web Api [英] Not able to consume MVC4 Web Api in HTML page

查看:140
本文介绍了无法在HTML页面中使用MVC4 Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在Asp.net MVC4中创建了Web API。

我使用了实体框架工作。我是能够在Json中获取数据,但我无法将这些数据与我的HTML页面集成。

我在html页面中使用过kendo UI控件(列表视图)。



这是我在Controller中写的:



Hi,

I have created Web API in Asp.net MVC4.
I have used entity frame work.As i am able to get data in Json but i am not able to integrate those data with my HTML page.
I have used kendo UI control (List view) in html page.

This is the I have written in Controller:

 public HttpResponseMessage get123()
        {
            return Request.CreateResponse(HttpStatusCode.OK, userlist());
        }


/-----To Get list of users-----/       
   
    private IEnumerable<userinfo> userlist()
        {
            userinfo[] list;
           using (var context = new Assignment_dbEntities())
            {
                var users = from userinfo in context.tblUserDetalis
                            select new userinfo()
                                {
                                    User = userinfo.Username,
                                };
                list = users.ToArray();
            }
            return list;

        }    


  public class userinfo
        {
            public string User { get; set; }
        }







从此代码获取此API网址: http:// localhost:58000 / api / Login / get123



现在我想在这个HTML页面中使用这个API:






From This Code i am getting this API url: http://localhost:58000/api/Login/get123

Now I am trying to use this API in this HTML page:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="styles/kendo.common.min.css" rel="stylesheet" />
    <link href="styles/kendo.default.min.css" rel="stylesheet" />
    <script src="js/jquery.min.js"></script>
    <script src="js/kendo.all.min.js"></script>
</head>
<body>
    <div id="example" class="k-content">

    <div class="demo-section">
        <div id="listView"></div>
        <div id="pager" class="k-pager-wrap"></div>
    </div>

    <script type="text/x-kendo-template" id="xyz">
        <div class="product">
            <h3>#:User#</h3>
        </div>
    </script>

    <script>
        $(function() {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "http://localhost:58000/api/Login/get123",
                        dataType: "jsonp"
                    }
                },
                pageSize: 2
            });

            $("#pager").kendoPager({
                dataSource: dataSource
            });

            $("#listView").kendoListView({
                dataSource: dataSource,
                template: kendo.template($("#xyz").html())
            });
        });
    </script>

    <style scoped>
        .demo-section {
            padding: 30px;
            width: 577px;
        }
        #listView {
            padding: 10px;
            margin-bottom: -1px;
            min-width: 555px;
            min-height: 510px;
        }
        .product {
            float: left;
            position: relative;
            width: 111px;
            height: 170px;
            margin: 0;
            padding: 0;
        }
        .product img {
            width: 110px;
            height: 110px;
        }
        .product h3 {
            margin: 0;
            padding: 3px 5px 0 0;
            max-width: 96px;
            overflow: hidden;
            line-height: 1.1em;
            font-size: .9em;
            font-weight: normal;
            text-transform: uppercase;
            color: #999;
        }
        .product p {
            visibility: hidden;
        }
        .product:hover p {
            visibility: visible;
            position: absolute;
            width: 110px;
            height: 110px;
            top: 0;
            margin: 0;
            padding: 0;
            line-height: 110px;
            vertical-align: middle;
            text-align: center;
            color: #fff;
            background-color: rgba(0,0,0,0.75);
            transition: background .2s linear, color .2s linear;
            -moz-transition: background .2s linear, color .2s linear;
            -webkit-transition: background .2s linear, color .2s linear;
            -o-transition: background .2s linear, color .2s linear;
        }
        .k-listview:after {
            content: ".";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }
    </style>
</div>

</body>
</html>







做完之后所有这些我能够在NET选项卡中看到firebugg中的json数据。

我也通过在浏览器中调用API来检查数据并且它正常运行。



查看以下结果:



[{User:admin},{User:nithya},{User: piyush},{User:govind},{User:shiva}]



帮助我解决这个问题我面临的问题从很长一段时间但没有得到任何解决方案。




After doing all these i am able to see json data in firebugg in NET tab.
I have also checked data by calling API in browser and its coming properly.

See these result:

[{"User":"admin"},{"User":"nithya"},{"User":"piyush"},{"User":"govind"},{"User":"shiva"}]

help me to solve this issue i am facing from long time but not getting any solution.

推荐答案

(function(){
var dataSource = new kendo.data.DataSource({
transport:{
read:{
url:http:// localhost:58000 / api / Login / get123,
dataType:jsonp
}
},
pageSize:2
});
(function() { var dataSource = new kendo.data.DataSource({ transport: { read: { url: "http://localhost:58000/api/Login/get123", dataType: "jsonp" } }, pageSize: 2 });


(#pager)。kendoPager({
dataSource:dataSource
});
("#pager").kendoPager({ dataSource: dataSource });


(#listView)。kendoListView({
dataSource:dataSource,
template:kendo.template(
("#listView").kendoListView({ dataSource: dataSource, template: kendo.template(


这篇关于无法在HTML页面中使用MVC4 Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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