1. weex中的get/post方法请求:weex中可使用axios请求,但是移动端不支持axios请求
//node.js中
//1. 处理get请求
app.get("/login",(req,res)=>{
var mphone = req.query.phone; //用户名
var mpwd = req.query.upwd; //密码
var sql = "SELECT mid FROM m_user";
sql += " WHERE mphone = ? AND mpwd = ?";
pool.getConnection((err,conn)=>{
if(err) throw err;
conn.query(sql,[mphone,mpwd],(err,result)=>{
if(err)throw err;
console.log(result)
if(result.length>0){
res.json({code:0,msg:"登录成功",result:result});
}else{
res.json({code:-1,msg:"用户名密码有误",result:result});
}
});
})
}
//2. 处理post请求:
app.post("/log",(req,res)=>{
var mphone = req.body.phone; //用户名
var mpwd = req.body.upwd; //密码
console.log(mphone)
console.log(req.body)
console.log(mpwd)
var sql = "SELECT mid FROM m_user";
sql += " WHERE mphone = ? AND mpwd = ?";
pool.getConnection((err,conn)=>{
if(err) throw err;
conn.query(sql,[mphone,mpwd],(err,result)=>{
if(err)throw err;
console.log(result)
if(result.length>0){
res.json({code:0,msg:"登录成功",result:result});
}else{
res.json({code:-1,msg:"用户名密码有误",result:result});
}
});
})
})
//封装get请求函数
var stream = weex.requireModule('stream')
export var weexRequestGet=(baseUrl,callback)=>{
return stream.fetch({
method: 'GET',
type: 'json',
url: baseUrl
}, callback)
}
// 封装post请求函数
export var weexRequestPost=(baseUrl,str,callback)=>{
return stream.fetch({
method: 'POST',
type: 'json',
headers:{'Content-Type':'application/json'},
body:JSON.stringify(str),
url: baseUrl
}, callback)
}
//1. 调用weex的get请求
var url="http://localhost:3000/login?phone="+this.phone+"&upwd="+this.code
weexRequestGet(url,(data)=>{
console.log(data)
})
//2. 调用weex的post请求
var url="http://localhost:3000/log"
var str={
phone:this.phone,
upwd:this.code
}
weexRequestPost(url,str,(data)=>{
console.log(data)
})
//注意:移动端无法通过扫码weex playground直接请求http://localhost:3000