快速解决HTML+JS开发的跨域问题

435 阅读1分钟

在本地开发html静态页面时,遇到请求接口的跨域问题

之前用vue开发,项目配合nginx进行代理转发解决跨域,

场景:使用原生HTML+JS开发,本地html的orgin为null 遇到跨域问题。

解决方法

使用live-server将本地html项目部署本地服务器上,结合Node解决跨域问题。

步骤

  1. 安装live-server
    NPM全局安装 
    npm install -g live-server
  2. 进入到项目文件夹
    npm init
    对项目构建得到package.json
  3. package.json进行配置

{  "name": "jg_control",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error:&nbspno&nbsptest&nbspspecified\" &&&nbspexit&nbsp1",    "start": "live-server&nbsp--open=/index.html&nbsp--port=7777&nbsp--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

成功啦~