Ajax基本使用二

前后端互相:



php:


如何从后端获取数据操作前端:

(忽视ajax部分代码,不想重新写了,后端仅传送了个5过来)

var button=document.querySelector('#btn'),
    div_fa=document.querySelector('#example');
var j=3;

function ajax(opts){
    var httpreq= new XMLHttpRequest(),
        url_con='';
    for(var key in opts.data){
        url_con+=key+'='+opts.data[key]+'&';
    }
    url_con = url_con.substr(0, url_con.length - 1);
    httpreq.onreadystatechange= function(){
        if(httpreq.readyState == 4&& httpreq.status== 200){
                opts.success(addline(httpreq.responseText));
        }
        if(httpreq.status== 403){
                opts.error();
        }
    }
    if(opts.type=='post'){
        httpreq.open(opts.type, opts.url, true);
        httpreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpreq.send(url_con);  
    }
    if(opts.type=='get'){
        httpreq.open(opts.type, opts.url+'?'+url_con, true);
        httpreq.send();     
    }
}

function addline(p){
    for(var i=0;i

php:


登录基本操作:


注册

  • 用户名:    只能是字母、数字、下划线,3-10个字符
  • 密码:    大写字母、小写、数字、下划线最少两种,6-15个字符
  • 再输一次: 

php


你可能感兴趣的:(Ajax基本使用二)