weiApi——返回Json

這裏寫圖片描述

html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#GetData").click(function () {
                $.ajax({
                    type: "get",
                    url: "/api/Test2/GetData",
                    success: function (data, status) { }
                });
            });

            $("#GetUser").click(function () {
                $.ajax({
                    type: "get",
                    url: "/api/Test2/GetUser",
                    success: function (data, status) { }
                });
            });
        });
    </script>
</head>
<body>
    <input type="button" id="GetData" value="返回值String" /> 
    <input type="button" id="GetUser" value="返回值Json" /> 
</body>
</html>
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace Hzb.File.Controllers
{
    public class Test2Controller : ApiController
    {
        public string GetData() 
        {
            return "abc";
        }

        public object GetUser()
        {
            User user = new User()
            {
                Name = "唐力",
                ID = 1,
                CreateTime = DateTime.Now
            };
            return user;
        }
    }


    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime CreateTime { get; set; }
    }
}

第一個按鈕返回值

這裏寫圖片描述

第二個按鈕返回值

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章