ajax请求
var ajax = new XMLhttpRequest();
ajax.open(method, url, false)
ajax.send()
ajax.onreadystatechange = function() {
if(ajax.readystate === 4 && ajax.status === 200) {
var data = JSON.parse(ajax.responseText);
console.log(data)
}
}
ajax请求封装
function ajax(options) {
var ajax = new XMLhttpRequest();
ajax.open(options.method, options.url, false)
ajax.send()
ajax.onreadystatechange = function() {
var data = JSON.parse(ajax.responseText);
if(ajax.readystate === 4 && ajax.status === 200) {
options.success(data)
} else if(ajax.readystate === 4 && ajax.status !== 200) {
options.fail(data)
}
}
}
ajax({
method: "get" || "post",
url: '你的请求地址',
success: function(res) {
},
fail: function(err) {
}
})
请求头方法解决express跨域问题
var express = require('express');
var router = express.Router();
router.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', '*')
next()
})