Ajax代码
//发送数据请求
ajax({
async:false,
url:'http://localhost:9000/register',
type:'POST',
dataType:'json',
data: JSON.stringify(data),
success:function(msg){
var json = JSON.parse(msg);
alert("注册成功!");
if (json.status == "true") {
window.location.href = "http://localhost:9000/login";
}
},
error:function (error) {
alert("error"+error.message);
}
});
返回数据
msg="{status:"true"}"
而且也不执行ERROR,就直接把success和error跳过了
创建Ajax函数,各位前端大佬,帮忙看一下!前端小白甚是感激!
//创建ajax函数
function ajax(options){
options=options||{};
options.type=(options.type||'GET').toUpperCase();
options.dataType=options.dataType||'json';
params = options.data;
//创建-第一步
var xhr;
//非IE6
if(window.XMLHttpRequest){
xhr=new XMLHttpRequest();
}else{
//ie6及其以下版本浏览器
xhr=ActiveXObject('Microsoft.XMLHTTP');
}
//接收-第三步
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status>=200 && xhr.status<300){
options.success&&options.success(xhr.responseText,xhr.responseXML);
}else{
options.error&&options.error(status);
}
}else {
// alert("系统繁忙,请稍后再试!")
}
}
//连接和发送-第二步
if(options.type==='GET'){
xhr.open('GET',options.url+'?'+params,true);
xhr.send(null);
}else if(options.type=='POST'){
xhr.open('POST',options.url,true);
//设置表单提交时的内容类型
xhr.setRequestHeader("Content-Type", "application/json;charset=utf-8");
xhr.send(params);
}
}