一、源生ajax GET请求
1、get请求代码:
function getXHR(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest;
}else if(window.ActiveXObject){
try{
xhr = new ActiveXObject('Msxml2.XMLHTTP')
}catch(e){
try{
xhr = new ActiveXObject('Microsoft.XMLHTTP')
}catch(e){
console.log('不支持ajax')
}
}
}
return xhr;
}
var xhr = getXHR();
xhr.open('get','http://localhost:8099/getTest?username=helen&age=29',true) //true代表异步操作
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var data = xhr.responseText;
console.log(JSON.parse(data),)
}
}
}
xhr.onerror = function(err){
console.log(err,'哦哦,出错啦啦啦啦了')
}
xhr.send()
node接口处理get请求
const express = require('express');
const app = express();
//const querystring = require('querystring');
//var bodyParser = require('body-parser'); //用来解析post请求发过来的参数
//app.use(bodyParser.json());
// 创建 application/x-www-form-urlencoded 编码解析
//app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('./public'));//用来访问静态资源
app.all('*', function(req, res, next) { //设置允许跨域访问
res.header("Access-Control-Allow-Origin","*");
//允许的header类型
res.header("Access-Control-Allow-Headers","content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() == 'options')
res.send(200); //让options尝试请求快速结束
else
next();
})
app.get('/',function(req,res){
res.send('Hello World!!!')
})
app.get('/getTest',function(req,res){ //测试自己写的源生ajax Get请求
console.log(req.query,'请求参数')
res.send({
msg: 'get请求成功,这个是返回给前端的结果!!',
data: {
text: '这是你传给我的数据,我原封不动还给你',
query: req.query
}
})
})
var server = app.listen(8099,function(){
var host = server.address().address;
var port = server.address().port;
console.log('应用实例,访问地址为http://%s:%s',host,port)
})
结果:
二、源生ajax POST请求
1.1、post请求代码(content-type:application/application/x-www-form-urlencoded,发送这个数据类型的话,node端可以通过body-parser模块来处理):
function getXHR(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest;
}else if(window.ActiveXObject){
try{
xhr = new ActiveXObject('Msxml2.XMLHTTP')
}catch(e){
try{
xhr = new ActiveXObject('Microsoft.XMLHTTP')
}catch(e){
console.log('不支持ajax')
}
}
}
return xhr;
}
var xhr = getXHR();
xhr.open('post','http://localhost:8099/postTest',true) //true代表异步操作
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var data = xhr.responseText;
console.log(JSON.parse(data),)
}
}
}
xhr.onerror = function(err){
console.log(err,'哦哦,出错啦啦啦啦了')
}
xhr.send('username=jack&age=28')
node 处理post请求:
const express = require('express');
const app = express();
const querystring = require('querystring');
var bodyParser = require('body-parser'); //用来解析post请求发过来的参数,前端发送的数据格式必须是:application/x-www-form-urlencoded,表单格式
//创建 application/x-www-form-urlencoded 编码解析
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('./public'));//用来访问静态资源
app.all('*', function(req, res, next) { //设置允许跨域访问
res.header("Access-Control-Allow-Origin","*");
//允许的header类型
res.header("Access-Control-Allow-Headers","content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() == 'options')
res.send(200); //让options尝试请求快速结束
else
next();
})
app.get('/',function(req,res){
res.send('Hello World!!!')
})
app.post('/postTest',function(req,res){
console.log(req.body,'post请求参数')
res.send({
msg: 'post请求成功,这个是给你的回应礼!!',
data: req.body
})
})
var server = app.listen(8099,function(){
var host = server.address().address;
var port = server.address().port;
console.log('应用实例,访问地址为http://%s:%s',host,port)
})
结果:
1.2 post请求(content-type:application/json)
function getXHR(){
var xhr = null;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest;
}else if(window.ActiveXObject){
try{
xhr = new ActiveXObject('Msxml2.XMLHTTP')
}catch(e){
try{
xhr = new ActiveXObject('Microsoft.XMLHTTP')
}catch(e){
console.log('不支持ajax')
}
}
}
return xhr;
}
var xhr = getXHR();
xhr.open('post','http://localhost:8099/postTest',true) //true代表异步操作
xhr.setRequestHeader('Content-Type','application/json')
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var data = xhr.responseText;
console.log(JSON.parse(data),)
}
}
}
xhr.onerror = function(err){
console.log(err,'哦哦,出错啦啦啦啦了')
}
var postdata = {
username: 'jack',
age:28,
height: 172
}
xhr.send(JSON.stringify(postdata))
node 处理post请求:
const express = require('express');
const app = express();
const querystring = require('querystring');
var bodyParser = require('body-parser'); //用来解析post请求发过来的参数,前端发送的数据格式必须是:application/x-www-form-urlencoded,表单格式
//创建 application/x-www-form-urlencoded 编码解析
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('./public'));//用来访问静态资源
app.all('*', function(req, res, next) { //设置允许跨域访问
res.header("Access-Control-Allow-Origin","*");
//允许的header类型
res.header("Access-Control-Allow-Headers","content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() == 'options')
res.send(200); //让options尝试请求快速结束
else
next();
})
app.get('/',function(req,res){
res.send('Hello World!!!')
})
app.post('/postTest',function(req,res){
var data = '';
req.on('data', function (chunk) {
// chunk 默认是一个二进制数据,和 data 拼接会自动 toString
data += chunk;
});
// 3.当接收表单提交的数据完毕之后,就可以进一步处理了
//注册end事件,所有数据接收完成会执行一次该方法
req.on('end', function () {
//(1).对url进行解码(url会对中文进行编码)
data = decodeURI(data);
console.log(data,'----data');
res.send({msg:'post请求处理成功!!',data: JSON.parse(data)})
});
})
var server = app.listen(8099,function(){
var host = server.address().address;
var port = server.address().port;
console.log('应用实例,访问地址为http://%s:%s',host,port)
})
结果: