快速解決HTML+JS開發的跨域問題

在本地開發html靜態頁面時,遇到請求接口的跨域問題

以前用vue開發,項目配合nginx進行代理轉發解決跨域,

場景:使用原生HTML+JS開發,本地html的orgin爲null 遇到跨域問題。

解決方法

使用live-server將本地html項目部署本地服務器上,結合Node解決跨域問題。html

步驟

  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"即要代理過去的地址vue

4.執行npm run startnginx

成功啦~npm

相關文章
相關標籤/搜索