欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CORS两种跨域方法示例讲解

程序员文章站 2022-03-07 22:14:43
ajax1.0 const http=require('http'); let httpserver=http.createserver((req,re...

ajax1.0

const http=require('http');

let httpserver=http.createserver((req,res)=>{
res.setheader('access-control-allow-origin','*');
res.write('aaaa');
res.end();
});

httpserver.listen(8080);

ajax2.0

const http=require('http');
let allowhosts=['taobao.com','baidu.com'];
let httpserver=http.createserver((req,res)=>{
if(allowhosts.indexof(req.headers['origin'])!=-1){
res.setheader('access-control-allow-origin','*');
}
res.write('aaaa');
res.end();
});

httpserver.listen(8080);