在本地开发html静态页面时,遇到请求接口的跨域问题
之前用vue开发,项目配合nginx进行代理转发解决跨域,
场景:使用原生HTML+JS开发,本地html的orgin为null 遇到跨域问题。
解决方法
使用live-server
将本地html项目部署本地服务器上,结合Node解决跨域问题。
步骤
- 安装live-server
NPM全局安装npm install -g live-server
- 进入到项目文件夹
npm init
对项目构建得到package.json - 对
package.json
进行配置
{ "name": "jg_control", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "live-server --open=/index.html --port=7777 --proxy=/transpond:http://192.168.20.57:18101/transpond" }, "author": "", "license": "ISC"}
proxy=/transpond:http://192.168.20.57:18101/transpond"
即要代理过去的地址
4.执行npm run start
成功啦~