1.服务端代码
var express = require('express')
var app = express()
var bodyParser = require('body-parser');
// 建立一个需要在同步作用域中执行的函数
var fibers = require('fibers');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// var host = "192.168.1.102"
var port = 8081
// 开启监听
// var server = app.listen(port,host,function(){
var server = app.listen(port,function(){
var host = server.address().address
var port = server.address().port
console.log('address === http://%s:%s',host,port)
})
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
fibers(function(){
next();
}).run();
});
// 处理get请求参数
app.get('/process_get',function(req,res){
// 输出 JSON 格式
var response = {
// req.query:获取URL的查询参数串
"first_name":req.query.first_name,
"last_name":req.query.last_name
};
console.log(response);
res.send(JSON.stringify(response))
// 中文部分乱码的问题
// res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});
// res.end(JSON.stringify(response));
})
// 处理post请求参数
app.post('/process_post', urlencodedParser, function (req, res) {
console.log("body === "+JSON.stringify(req.body))
console.log("query === "+JSON.stringify(req.query))
// 输出 JSON 格式
var response = {
// 获得「请求主体」
"first_name":req.body.first_name,
"last_name":req.body.last_name
};
console.log(response);
res.send(JSON.stringify(response))
// res.end(JSON.stringify(response));
})
2.客户端代码
封装在Http.js
文件中
var host_url = "http://192.168.50.163"
var port = "8081"
var NETWORK_TIMEOUT = 10*1000
var sendRequest = function(url,param,callback,type){
var send_url = host_url + ":" + port + url
var send_type = type ? type : "POST"
var send_param = ""
for(var key in param){
var value = param[key];
send_param += key;
send_param += "=";
send_param += value;
send_param += "&";
}
send_param = send_param.substring(0,send_param.length-1);
HttpRequest(send_url,send_param,callback,send_type)
}
var HttpRequest = function(url,param,callback,type){
console.log("===========================")
console.log("start http !!!")
console.log("网络传输:type === "+type)
console.log("网络传输:url === "+url)
console.log("网络传输:param === "+param)
console.log("===========================")
var xhr = cc.loader.getXMLHttpRequest();
xhr.timeout = NETWORK_TIMEOUT;
xhr.open(type,url);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){
switch(xhr.readyState){
case 0:
console.log("请求没有发出,在调用open()函数之前为该状态。")
break;
case 1:
console.log("请求已经建立但还没有发出,在调用send()函数之前为该状态。");
break;
case 2:
console.log("请求已经发出正在处理中。");
break;
case 3:
console.log("请求已经处理,响应中通常有部分数据可用,但是服务器还没有完成响应。");
break;
case 4:
if (xhr.status == 200) {
console.log("===========================")
console.log("响应已完成,可以访问服务器响应并使用它。");
console.log("response::")
console.log(xhr.responseText)
console.log("===========================")
var reponse_obj = JSON.parse(xhr.responseText);
callback(reponse_obj)
}else{
switch(xhr.status){
case 502:
console.log('502已断连......')
break;
case 500:
console.log('500已断连......')
break;
default:
console.log(xhr.status+' === 响应已完成,但回调失败。')
break;
}
}
break;
}
}
xhr.ontimeout = function(){
console.log("===========================");
console.log("请求超时");
}
xhr.onerror = function(){
console.log("===========================");
console.log("无法连接到服务器");
}
xhr.send(param)
}
exports.sendRequest = sendRequest;