axiosを用いてサーバにリクエストを送信しているのですが、CORS policyによってブロックされてしまいます。
以下がaxiosを用いてリクエストを送信しているコードです。
html
1<!DOCTYPE html> 2<html lang="ja"> 3 4<head> 5 <meta charset="UTF-8"> 6 <title>Document</title> 7</head> 8 9<body> 10 <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> 11 <script> 12 axios.get("http://localhost:7500/lessons?id=13").then(response => { 13 console.log(response); 14 }) 15 </script> 16</body> 17 18</html>
以下がサーバのコードです。
go
1package main 2 3import ( 4 "net/http" 5) 6 7func f(w http.ResponseWriter, r *http.Request) { 8 w.Header().Set("Access-Control-Allow-Origin", "*") 9} 10 11func main() { 12 http.HandleFunc("/lessons/", f) 13 http.ListenAndServe(":7500", nil) 14} 15
以下が発生しているエラーメッセージです。
Access to XMLHttpRequest at 'http://localhost:7500/lessons?id=13' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
サーバの以下のコードを
go
1http.HandleFunc("/lessons/", f)
次のコードに
go
1http.HandleFunc("/lessons", f)
変更すればブロックされないのですが「lessons/1」などのパスにもマッチさせたいため、できればサーバのコードを変更せずに解決したいと考えています。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/14 01:19