Slog35_支配vue框架初阶项目之博客网站-注册页面-展示注册结果

  • ArthurSlog
  • SLog-35
  • Year·1
  • Guangzhou·China
  • Aug 11th 2018

  • GitHub
  • 掘金主页
  • 简书主页
  • segmentfault

减少做一些“没脑子”的决定 学会用用宏观代替直观 用数据代替感受 用“慢思考”代替“快思考”


开发环境MacOS(High Sierra 10.13.5)

需要的信息和信息源:

  • HTTP概述
  • HTTP
  • 互联网是如何工作的
  • 万维网是如何工作的
  • 统一资源定位符(URL)
  • 什么是超链接
  • 创建超链接
  • AJAX是异步的JavaScript和XML(Asynchronous JavaScript And XML)
  • XMLHttpRequest
  • Using files from web applications

开始编码

  • 之前我们使用 vue.js 框架,同时参考了 webAPI
  • 然后在 js 文件里使用 XMLHttpRequest 对象
  • js 文件操作浏览器通过 http协议 向服务器传递用户输入的信息数据,这样实现了一个用户注册的功能
  • 服务端把客户端(浏览器)传送过来的数据,存储进数据库,然后给客户端(浏览器)返回一个执行结果
  • 本篇就是从服务端获取注册功能的执行结果,然后在页面上显示出来
  • 下面给出修改后的代码

signup.html





    
    
    
    
    signup_ArthurSlog




    
This is signup's page by ArthurSlog

Singup


Account: {{ name }}


Password: {{ password }}


Again Password: {{ repassword }}


First Name: {{ firstname }}


Last Name: {{ lastname }}


Birthday: {{ birthday }}


Sex: {{ currentSex }}



Age: {{ currentAge }}


Wechart: {{ wechart }}


QQ: {{ qq }}


Email: {{ email }}


Contury: {{ contury }}


Address: {{ address }}


Phone: {{ phone }}


Websize: {{ websize }}


Github: {{ github }}


Bio: {{ bio }}


Return index's page

{{ commits }}
  • 再看一下 js 文件

signup.js

var host = 'http://127.0.0.1:3000/signup?';

var signup_container = new Vue({
    el: '#signup-container',
    data: {
      name: '',
      password: '',
      repassword: '',
      firstname: '',
      lastname: '',
      birthday: '',
      sexs: ['male', 'female'],
      currentSex: 'male',
      ages: ['1', '2', '3', '4', '5', '6', '7', '8',
             '9', '10', '11', '12', '13', '14', '15', '16', '17', '18'],
      currentAge: '18',
      wechart: '',
      qq: '',
      email: '',
      contury: '',
      address: '',
      phone: '',
      websize: '',
      github: '',
      bio: '',
      commits: null
    },
    methods: {
      addUser: function () {
        var xhr = new XMLHttpRequest()

        var self = this
        xhr.open('GET', host + 'name=' + self.name + '&password=' + self.password + '&firstname=' + 
        self.firstname + '&lastname=' + self.lastname + '&birthday=' + self.birthday
        + '&sex=' + self.currentSex + '&age=' + self.currentAge + '&wechart=' + self.wechart
        + '&qq=' + self.qq + '&email=' + self.email + '&contury=' + self.contury
        + '&address=' + self.address + '&phone=' + self.phone + '&websize=' + self.websize
        + '&github=' + self.github + '&bio=' + self.bio, true)
        
        xhr.onload = function () {
          self.commits = xhr.responseText
        }
        
        xhr.send()
      }
    }
  })
  • 注意看到 html 文件里新添加的部分
{{ commits }}
  • 我们根据 vue.js 框架,在这里双大括号里的 commits 与 js 文件里的 commits 相关联
  • 然后我们希望当服务端把执行结果 完整的 返回的客户端(浏览器)之后,把这个返回的值 赋予 commits 这个对象,然后重新渲染页面(vue.js 做的事情),这样我们在页面上就可以看到执行了结果了,也就知道注册是成功或者失败了
  • 当然,在这里,我们之前编写服务端代码的时候,在逻辑里写了“如果注册成功,返回‘Signup Successful!’这样的字符串”,所以,当注册成功的时候,我们在页面上看到的,就会出现 “Signup Successful!”
  • 现在,我们看下 js 文件里的改动:
methods: {
    addUser: function () {
    var xhr = new XMLHttpRequest()

    var self = this
    xhr.open('GET', host + 'name=' + self.name + '&password=' + self.password + '&firstname=' + 
    self.firstname + '&lastname=' + self.lastname + '&birthday=' + self.birthday
    + '&sex=' + self.currentSex + '&age=' + self.currentAge + '&wechart=' + self.wechart
    + '&qq=' + self.qq + '&email=' + self.email + '&contury=' + self.contury
    + '&address=' + self.address + '&phone=' + self.phone + '&websize=' + self.websize
    + '&github=' + self.github + '&bio=' + self.bio, true)
    
    xhr.onload = function () {
        self.commits = xhr.responseText
    }
    
    xhr.send()
    }
}
  • 其中,把 xhr.open() 这个函数执行完的返回值 赋予 self 这个对象
  • 然后,根据 webAPI,可知:
XMLHttpRequestEventTarget.onload 是 XMLHttpRequest 请求成功完成时调用的函数。

callback 是请求成功完成时要执行的函数。它接收一个 ProgressEvent 对象作为它的第一个参数。this 的值(即上下文)与此回调的 XMLHttpRequest 相同。
  • 使用示例:

XMLHttpRequestEventTarget.onload

var xmlhttp = new XMLHttpRequest(),
  method = 'GET',
  url = 'https://developer.mozilla.org/';

xmlhttp.open(method, url, true);
xmlhttp.onload = function () {
  // 处理取回的数据(在 xmlhttp.response 中找到)
};
xmlhttp.send();
  • ok,现在启动你的服务器(server路径下)
node index.js
  • 然后,打开浏览器,输入 127.0.0.1:3000,进入注册界面,填写完你的注册信息,点击“完成注册”按钮
  • 现在,在页面的最下端,应该出现了一行新的字符串,他代表了你刚刚的注册行为已经成功完成!
Singup Successful!
  • 至此,我们在使用 vue.js 框架的情况下,实现了 展示注册结果的功能。

欢迎关注我的微信公众号 ArthurSlog

如果你喜欢我的文章 欢迎点赞 留言

谢谢

你可能感兴趣的:(web,koa.js,vue.js)