網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
【錯(cuò)誤記錄/html】Response to preflight request doesn‘t pass access control check: No ‘Access-Control-Allow
作者:o0o_-_ 更新時(shí)間: 2022-03-14 編程語(yǔ)言錯(cuò)誤詳情
- 在使用
ajax
向http
服務(wù)器請(qǐng)求時(shí),出現(xiàn)以下錯(cuò)誤:Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
- 請(qǐng)求如下:
$.ajax({ url: "http://127.0.0.1/getname", type: 'GET', success: function (data) { UpdateNameInput(htmlDomInputID, data); }, error: function () { console.log("Get Rand Name Failed!"); } });
- 服務(wù)器為
golang
實(shí)現(xiàn)
一種解決
- 查了好多資料,嘗試了好多方法,最終用了這個(gè)_StackOverflow
- 即
服務(wù)器try { var xhttp = new XMLHttpRequest(); xhttp.open("GET", httpURL + httpGetName, false); xhttp.setRequestHeader("Content-type", "text/html"); xhttp.send(); alert(xhttp.response) } catch (error) { alert(error.message); }
func GetNameHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Headers", "Content-Type") randName := "NickName" fmt.Fprintf(w, randName) }
另一種方法
- golang庫(kù)-cors
- 使用
服務(wù)器
jsfunc StartHttpServer() bool { c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, }) mux := http.NewServeMux() mux.HandleFunc("/getname", GetIDHandler) handler := c.Handler(mux) http.ListenAndServe(addr, handler) return true }
function GetRandName() { $.ajax({ url: "http://127.0.0.1/getname", type: 'GET', success: function (data) { console.log(data) UpdateNameInput(htmlDomInputID, data.id); }, error: function () { console.log("Get Rand Name Failed!"); } }); }
原文鏈接:https://blog.csdn.net/qq_33446100/article/details/107992701
相關(guān)推薦
- 2022-09-25 ORACLE數(shù)據(jù)庫(kù)數(shù)據(jù)泵的導(dǎo)入導(dǎo)出
- 2022-03-28 Python實(shí)現(xiàn)網(wǎng)頁(yè)文件轉(zhuǎn)PDF文件和PNG圖片的示例代碼_python
- 2022-05-12 tp5 二級(jí)項(xiàng)目目錄配置nginx偽靜態(tài)
- 2022-07-09 常見(jiàn)的dos命令
- 2022-10-03 Golang?Http請(qǐng)求返回結(jié)果處理_Golang
- 2022-09-26 在?React?Native?中使用?CSS?Modules的配置方法_React
- 2022-09-07 在VScode中配置ROS環(huán)境的詳細(xì)過(guò)程_相關(guān)技巧
- 2022-11-23 詳解如何使用Pytorch進(jìn)行多卡訓(xùn)練_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支