9.5cors配置代码


server 8081端口 let http = require('http'); let server = http.createServer((req, res) => { console.log("服务端已开启--8081端口") res.writeHead(200, { 'Content-Type': 'text/plain;charset=utf-8',//utf-8编码 'Access-Control-Allow-Origin': 'http://localhost:8080', //"*"表示任意字段 'Access-Control-Expose-Headers':'Cache-Control', 'Access-Control-Allow-Headers':'X-Custom-Header', 'Access-Control-Allow-Credentials': true, 'Access-Control-Allow-Methods': 'PUT,GET,POST' }) res.end('这是
https://blog.csdn.net/qq_36996271/article/details/89762667?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.edu_weight&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.edu_weight

 

8081端口返回的数据')

}).listen(8081)

  

8080端口

<body>
  <div class="">我在8080端口,请求8081端口的数据</div>
  <script type="text/javascript">
		let xhr = new XMLHttpRequest()
    xhr.open('put','http://localhost:8081',true);
    xhr.setRequestHeader('X-Custom-Header', 'value');
    xhr.withCredentials = true;
    //xhr.setRequestHeader('X-Custom-Header', 'value')
    xhr.onload = function(res){
      // 获取数据
      console.log(xhr.responseText,res);
      // 修改页面的dom元素
      console.log(xhr.getAllResponseHeaders(Headers))
    }
  
    xhr.onerror = function(err) {
      console.log(err)
    }
		xhr.send()
  </script>


let http = require('http');
let fs = require('fs')
let server = http.createServer((req, res) => {
	console.log("客户端已开启--8080端口")
	// 读取HTML文件
	let html = fs.readFileSync('./user-agent.html', 'utf8')
	res.writeHead(200, {
		'Content-Type': 'text/html'
	})
	// 将读取到的HTML文件写入到响应流中
	res.end(html)
}).listen(8080)

  

posted @ 2020-09-05 16:06  尽世间恶丑  阅读(218)  评论(0编辑  收藏  举报