Ajax

GET方式:

创建 ajax 对象
var ajax = new XMLHttpRequest();
向服务器发起请求
ajax.open('GET','/checkuser',true);
ajax.send();

响应
ajax.onreadystatechange=function(){
if(ajax.readyState == 4&& ajax.status == 200){
var d = ajax.responseText;
}
}

GET接口:

router.get('/checkuser',function(req,res){

var name = req.query;

if(name.username == 'abc'){

res.json({ok:0});

}else{

res.json({ok:1});

};
})




POST方式的:

var ajax = new XMLHttpRequest();

ajax.open('post','/aa');

ajax.setRequestHeader('content-type','application/x-www-form-urlencoded');

ajax.send('username='+user.value);

ajax.onreadystatechange = function(){
if(ajax.readyState == 2 &&a jax.status == 200){

var res = ajax.responseText;

};
};

POST接口:

router.post('/aa',function(req,res){

var d = req.body;

res.json({ok:1});

};);




Jquery方式的ajax:

$(function(){
$('#btn').on('click',function(){
$.ajax({
url:'/data1',
data:{name:123},
type:'get',
timeout:5000,   //请求超时停止请求
success:function(msg){
$.each(msg.data,function(k,v){
var li = $('<li>');
li.text(v);
$('#list').append(li);
});
}
})
})
})

接口:

router.get('/',function(req,res){
res.render('aaa');
})

router.get('/data1',function(req,res){
var d = req.query;
res.json({data:['aaaaa','bbbbb','cccc']})
});



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭