欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

前端解決跨域問題nmp安裝http-server的問題

 更新時間:2022年07月25日 10:12:30   作者:BUZHOU  
這篇文章主要介紹了前端解決跨域問題-nmp安裝http-server的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前端解決跨域問題-nmp安裝http-server

問題背景

項目組的露露學習d3遇到問題,前端無法讀取js中調(diào)用的csv文件

于是誠摯地邀請露露來了我的寢室坐坐,嘗試了相對路徑與絕對路徑均無效,且絕對路徑復制進chrome可直接下載,即文件路徑?jīng)]錯。

右鍵“檢查”進入開發(fā)者模式看報錯,

報錯:Access to XMLHttpRequest at 'file:///C:/Users/buzou/Desktop/OneNum/OneNum.csv' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

搜索得知為跨域問題

跨域問題

跨域問題: 在本場景下,指的是俠義的跨域問題。

由瀏覽器同源策略限制的一類請求場景。

什么是同源策略?
同源策略/SOP(Same origin policy)是一種約定,由Netscape公司1995年引入瀏覽器,它是瀏覽器最核心也最基本的安全功能,如果缺少了同源策略,瀏覽器很容易受到XSS、CSFR等攻擊。所謂同源是指"協(xié)議+域名+端口"三者相同,即便兩個不同的域名指向同一個ip地址,也非同源。

大概看了Google的各種結(jié)果,嘗試了一下兩個看上去最容易的辦法

嘗試方法一:chrome快捷方式加” --allow-file-access-from-files“無效

嘗試方法二:嘗試使用nmp安裝本地服務器

node.js實際上我也只是安裝了但不熟悉,在配置的過程中仍然有幾個坑,具體步驟跟著下面的鏈接來就好了:

https://jasonwatmore.com/post/2016/06/22/nodejs-setup-simple-http-server-local-web-server

(講的全面且權(quán)威)

踩過的坑就是http-server要下載到全局,用這條命令安裝npm install -g http-server,

在對應的d3(前端文件)目錄下輸入http-server啟動,會看到它給出網(wǎng)址,復制“127.0.0.1”這一條到瀏覽器,能進入服務器(后臺)頁面。

后記

這個寫的看起來挺簡單的但實際上還是卡了一個多小時,心得就是幫美女解決問題不要慌,不能心猿意馬。

后續(xù)寫博客記錄再搜索了一下,https://zhuanlan.zhihu.com/p/130391287中寫了chrome版本使用場景: 80.0.3987.163(正式版本) (64 位)可以通過指定“ --allow-file-access-from-files”來繞過跨域問題。

補充:下面看下http-server服務跨域設置問題

http-server --cors -p 9999

http-server --cors -p 9999 -c-1

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Available Options:
-p Port to use (defaults to 8080)

-a Address to use (defaults to 0.0.0.0)

-d Show directory listings (defaults to 'True')

-i Display autoIndex (defaults to 'True')

-g or --gzip When enabled (defaults to 'False') it will serve ./public/some-file.js.gzin place of ./public/some-file.js when a gzipped version of the file exists and the request accepts gzip encoding.

-e or --ext Default file extension if none supplied (defaults to 'html')

-s or --silent Suppress log messages from output

--cors Enable CORS via the Access-Control-Allow-Origin header

-o Open browser window after starting the server

-c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1.

-U or --utc Use UTC time format in log messages.

-P or --proxy Proxies all requests which can't be resolved locally to the given url. e.g.: -P http://someurl.com

-S or --ssl Enable https.

-C or --cert Path to ssl cert file (default: cert.pem).

-K or --key Path to ssl key file (default: key.pem).

-r or --robots Provide a /robots.txt (whose content defaults to 'User-agent: *\nDisallow: /')

-h or --help Print this list and exit.

到此這篇關于前端解決跨域問題-nmp安裝http-server的文章就介紹到這了,更多相關nmp安裝http-server內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論