First Knowledge Vue 2.0 (3): Ajax request

Source: Internet
Author: User

0, to simulate the AJAX request, need to build a Web program, using express easy and fast.

1. Install Express build Web server:

NPM install-g expressnpm install-g express-generator

2. Create an Express project:

3, start the project, generally can use:

NPM start

However, the development environment needs to monitor code changes and automatically restart the process, so you need to install supervisor.

NPM install-g Supervisor

Start with supervisor:

Supervisor./bin/www

For ease of administration, create a Start.bat startup script in the project root directory.

Start supervisor./bin/www

4, modify the Express default router directory of Users.js, the requested get parameters, converted to JSON directly return ^_^

var express = require (' Express '); var router = Express. Router ();/* GET users listing. */router.get ('/', function (req, res, next) {
Res.header ("Access-control-allow-origin", "*");//solve cross-domain problem res.end (json.stringify (Req.query));}); Module.exports = router;

At this point, the Web server fights, can provide a simple data interface services, the effect is as follows:

5, build front-end AJAX requests, install Axiso:

NPM Install-save Axios

6, introduced in Main.js:

Import Axios from ' Axios ' vue.prototype. $ajax = Axios

7. Rewrite the game module:

Export Default {  name: ' Game ',  data () {    return {      msg: '    }  },  mounted:function () { This.getdata ()  },  methods:{getdata:function () {var _this = this;this. $ajax. Get (' Http://localhost:3000/ users?username=tree&password=123456 '). Then (function (response) {console.log (response.data); _this.msg= " I am the game module, user name: "+response.data.username});}}}  

Start the front-end development mode, you can observe the browser network has an interface request, the display effect is as follows:

Of course, this is just a demo, next we will explain the event ^_^

First Knowledge Vue 2.0 (3): Ajax request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.