cors跨域相关
CORS跨域详解:点击查看日志
CORS技术详解二:点击查看
node.js
var http = require('http'); var fs = require('fs'); var url = require('url'); var server = http.createServer(); server.listen(9000, function () { console.log('9000') }); server.on('request', function (req, res) { var urls = url.parse(req.url, true); // cors 跨域的核心点 // cors 允许跨域的来源域名 如果值为* 允许所有外域发送请求 res.setHeader('Access-Control-Allow-Origin', '*'); if (req.method == 'GET') { if (urls.pathname == '/') { res.end('gggeettt') } else if (urls.pathname == '/gets') { res.end('999') } else if (urls.pathname == '/kuayus') { var fun_name = urls.query.callback; // console.log('23'); var json_str = '[{"id":"2","name":"lisii"}]' // kk([{"id":"2","name":"lisii"}]) res.end(fun_name + '(' + json_str + ')') } else { fs.readFile('.' + urls.pathname, function (err, data_str) { if (!err) { res.end(data_str); } else { // console.log(err); res.end(''); } }) } } else if (req.method == 'POST') { } else if (req.method == 'OPTIONS') { // cors 跨域有两种请求方式 // 1:简单请求 // 2:需预检请求 // 2中需要先发送 OPTIONS 请求,来征得服务器的同意后才会发送真正的请求 res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE'); res.end(); } else { res.end('methods'); } })
php代码中添加一下header头声明:
Access-Control-Allow-Origin:* //域名,* 允许所有
php:(服务端代码)
header('Access-Control-Allow-Origin:http://localhost');

浙公网安备 33010602011771号