node
var http = require('http');
var url = require('url')
var server = http.createServer(function (req, res) {
var params = url.parse(req.url, true).query;
str = {
id:params.id,
name:params.name
}
var aa = JSON.stringify(str)
res.writeHeader(200, { "Content-Type": 'text/plain', 'charset': 'utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS' });
//大长串解决跨域
res.write(aa);
//返回的数据必须是string或者buffer,不然一直报错,ajax端设置为json
res.end()
}).listen(3300);
console.log('server is running at port 3300');
ajax
get: function (url, config, callback, err) {
let fullurl = url
if (!!config) {
fullurl = url + "?"
for (let prop in config) {
fullurl = fullurl + "&" + prop + "=" + config[prop]
}
}
$.ajax({
type: "get",
url: fullurl,
dataType: "json",
success: function (data) {
callback(data)
},
error: function (data) {
if (!!err) {
err(data)
}
}
})
}
网友评论