.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>跨域服务器端解决</title>
</head>
<body>
<!--
在终端中运行服务器文件: node crane-serve.js
右击“Open with Live Server”运行本程序代码
-->
<script src="./jquery-1.12.4.js"></script> //引入js文件
<script>
//约定端口号为3600, ip地址是运行服务器文件的那台主机ip
//在cmd命令行窗口输入ipconfig获取本机IP地址
$.get('http://10.10.11.233:3600/', function(r) {
console.log('连接成功')
})
</script>
</body>
</html>
服务器文件
let http = require('http')
http.createServer((req, res) => {
//添加下面这行代码,就可以解决跨域问题
res.setHeader('Access-Control-Allow-Origin', '*')
res.end('success')
}).listen(3600, function() { //上个文件中约定端口为3600
console.log('服务开启成功...')
})