本地增删查的一个例子

<div id="box">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">新增</h3>
        </div>
        <div class="panel-body form-inline">
            <label>
                id:<input type="text" class="form-control" v-model="obj.id" />
            </label>
            <label>
                name:<input type="text" class="form-control" v-model="obj.name" />
            </label>
            <input type="button" value="add" class="btn btn-primary" @click="add" />
            <label>
                search:<input type="text" class="form-control" v-model="keyWords"/>
            </label>
        </div>
    </div>
    <table class="table table-striped table-bordered table-hover table-condensed">
        <tr><th>id</th><th>name</th><th>createTime</th><th>opration</th></tr>
        <!--//呈现的数据由vue对象内部维护的search函数提供,当在搜索框输入数据后,vue对象内部数据keyWords发生变化,则会自动调用search进行数据过滤-->
        <tr v-for="obj in search(keyWords)" :key="obj.id"><td>{{obj.id}}</td><td>{{obj.name}}</td><td>{{obj.time}}</td><td><a href="#" @click.prevent="del(obj.id)">删除</a></td></tr>
    </table>
</div>
vm = new Vue({
    el: "#box",       
    data: {
        keyWords: "",
        obj: { id: null, name: "", time: null },
        list: [
            { id: 1, name: "sam",time:new Date() },
            { id: 2, name: "leo", time: new Date() },
            { id: 3, name: "korn", time: new Date() }
        ]
    },
    methods: {
        add: function () {
            var id = this.obj.id;
            var name = this.obj.name;
            this.list.push({ id: id, name: name,time:new Date() });
        },
        del: function (id) {
            var index=this.list.findIndex((item) => {
                if (item.id == id) return true;
            });
            this.list.splice(index, 1);
        },
        search: function (keyWords) {
            return this.list.filter((item,index) => {
                if (item.name.includes(keyWords)) {
                    return item;
                }
            });             
        }
    }
});

向服务端发起请求

axios

这是官方推荐的新的插件,用于客户端向服务端发送请求。可在axios下载。

<div id="box">
    {{msg}}
    <button @click="sendGet">GetInfo</button> 
        }
    },
    methods: {       
        sendGet: function () {
            var self=this;
            var getObj=axios.get("http://localhost:53385/Handler.ashx", {
                params: self.sendData    
            });
            var endObj= getObj.then(function (response) {
                self.msg = response.data;
            });
            endObj.catch(function (error) {
                self.msg = error.status + error.statusText;
            });
        },
        sendPost: function () {
            var self = this;
            var postObj = axios.post("http://localhost:53385/Handler.ashx", self.sendData);
            var endObj=postObj.then(function (response) {
                self.msg = response.data;
            });
            endObj.catch(function (error) {
                self.msg = error.status + error.statusText;
            });
        }
    }
});
</script>

服务端

ublic class Handler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("OK");
        context.Response.End();
    }  
}

增删改查实例

<div id="box">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加</h3>
        </div>
        <div class="panel-body form-inline">
            <label>
                name:<input type="text" class="form-control" v-model="name" />
            </label>
            <input type="button" value="新增" class="btn btn-primary" @click="add" />
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Date</th>
            <th>Operation</th>
        </tr>
        <tr v-for="obj in list" :key="obj.ID">
            <td>
                {{obj.ID}}
            </td>
            <td>
                {{obj.Name}}
            </td>
            <td>
                {{obj.Date}}
            </td>
            <td>
                <a href="#" :id="obj.ID">编辑</a>
            </td>
        </tr>
    </table>
</div>

服务端需要创建一个模拟数据库存储数据的在内存中可以保持全局唯一的数据对象以便可以在这个对象列表里持续添加数据

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public static List<Employee> GlobalList { get; set; } //全局唯一的员工列表

static Employee()
    {
        GlobalList= new List<Employee>
        {
            new Employee
            {
                ID = 1,
            Date = new DateTime(),
            Name = "寒食"
            },
            new Employee
            {
                ID = 2,
            Date = new DateTime(),
            Name = "山魈"
            },
                new Employee
            {
                ID = 3,
                Date = new DateTime(),
                Name = "李隼"
            }
        };
    }
}

查询

客户端代码

var vm = new Vue({
    el: "#box",
    data: {
        name:null,
        list: [
            { ID: 1, Name: "sam", Date: new Date() }
        ]
    },
    methods: {
        getServerData: function () {
            var self = this;
            axios.get("http://localhost:53385/getAllList.ashx").then(function (response) {
                self.list = response.data;
            });
        }
    },
    created: function () { //vue对象创建完成后开启查询
        this.getServerData();
    }
});

服务端代码

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string listStr = JsonConvert.SerializeObject(Employee.GlobalList);
    context.Response.Write( listStr);
    context.Response.End();
}

新增

客户端代码

var vm = new Vue({
    el: "#box",
    data: {
        name:null,
        list: [
            { ID: 1, Name: "sam", Date: new Date() }
        ]
    },
    methods: {            
        add: function () {
            var self = this;
            axios.post("http://localhost:53385/addData.ashx", { name: this.name }).then(function (response) {
                self.list = response.data;
            });
        }
    }      
});

服务端代码

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    //客户端axios对象的post方法模拟了post提交方式,但它提交的是json格式的数据,不能用request.form[]来获取数据
    //以下通过读取流数据来获取客户端提交的json数据
    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    using (var reader = new System.IO.StreamReader(context.Request.InputStream))
    {
        String jsonData = reader.ReadToEnd();
        var em = new Employee { Date = new DateTime(), Name = JsonConvert.DeserializeObject<Employee>(jsonData).Name };
        var list = Employee.GlobalList;
        list.Add(em);
        var message = string.IsNullOrEmpty(jsonData) ? "error" : JsonConvert.SerializeObject(list);
        context.Response.Write(message);
        context.Response.End();
    }
}

删改略……

Javascript - 学习总目录

Javascript - Vue - 请求的更多相关文章

  1. Javascript异步请求你能捕获到异常吗?

    Javascript异步请求你能捕获到异常吗? 异常处理是程序发布之前必须要解决的问题,不经过异常处理的应用会让用户对产品失去信心.在异常处理中,我们一贯的做法是按照函数调用的次序,将异常从数据访问层 ...

  2. 一个vue请求接口渲染页面的例子

    new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...

  3. JavaScript获取请求参数

    <script type="text/javascript"> //获取请求参数 function paramsMap() { var url = window.loc ...

  4. Javascript - Vue - vue对象

    vue提供了一整套前端解决方案,可以提升企业开发效率 vue的处理过程 app.js 项目入口,所有请求最先进入此模块进行处理 route.js 由app.js调用,处理路由的分发 controlle ...

  5. Javascript - Vue - 在vscode里使用webpack

    cnpm(node package manager)和webpack模块 npm是运行在node.js环境下的包管理工具,使用npm可以很快速的安装前端文件里需要依赖的那些项目文件,比如js.css文 ...

  6. Javascript - Vue - 路由

    vue router.js 下载:vue-router.js, 该文件依赖于vue.js <script src="Scripts/vue-2.4.0.js">< ...

  7. Javascript - Vue - webpack + vue-cil

    cnpm(node package manager)和webpack模块 npm是运行在node.js环境下的包管理工具(先安装node.js,再通过命令 npm install npm -g 安装n ...

  8. Javascript - Vue - webpack中的组件、路由和动画

    引入vue.js 1.cnpm i vue -S 2.在mian.js中引入vue文件 import Vue from "vue"//在main.js中使用这种方式引用vue文件时 ...

  9. VUE请求本地数据的配置json-server

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

随机推荐

  1. hexo发文章

    http://blog.csdn.net/qq_36099238/article/details/54576089

  2. 冲刺Two之站立会议6

    今天继续了昨天的工作,对视频进行优化.因为昨天的工作没有达到预期的效果,所以又继续对音质和画面质量做了相应的优化.还对相应的聊天室界面进行了优化.

  3. Studying GIT

    Studying git Shopping list: GIT的功能 接触一个新的软件或者网站,最重要的就是认识它的功能:Git 是用于 Linux内核开发的版本控制工具.与常用的版本控制工具 CVS ...

  4. 【转】mybatis如何防止sql注入

    sql注入大家都不陌生,是一种常见的攻击方式,攻击者在界面的表单信息或url上输入一些奇怪的sql片段,例如“or ‘1’=’1’”这样的语句,有可能入侵参数校验不足的应用程序.所以在我们的应用中需要 ...

  5. bower安装和使用

    bower的安装 1,首先在我的系统 安装 nodejs.因为我的系统是windows,还需要安装msysgit,注意图二中的选项   msysgit   Git setup 2,之后就可以用npm包 ...

  6. CF86D Powerful array

    题意翻译 题意:给出一个n个数组成的数列a,有t次询问,每次询问为一个[l,r]的区间,求区间内每种数字出现次数的平方×数字的值 的和. 输入:第一行2个正整数n,t. 接下来一行n个正整数,表示数列 ...

  7. bzoj 1050 [HAOI2006]旅行comf (并查集)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 思路: 先将每条边的权值排个序优先小的,然后从小到大枚举每一条边,将其存到并查集 ...

  8. 【刷题】洛谷 P1966 火柴排队

    题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...

  9. Python3中的编码问题

    编码方式介绍 我们首先来熟悉一下常见的一些编码方式,按照时间轴来划分,共有以下几种编码方式(常见): ASCII编码方式:对127个常见字符进行编码,其中包含了10个阿拉伯数字,共52个大小写英文字母 ...

  10. 高阶函数map(),filter(),reduce()

    接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...