前提
React + Axios + FastAPIでWEBアプリを作っています。
axiosでlocalhostに対してGETを行っても、サーバー側では200 OKと表示されているのに、ブラウザ側ではエラーとなり値が受け取れません。
reactはnpxのcreate-react-appで作成し、npm startでlocalhost:3000にて動いています。
FastAPIはlocalhost:8000で動いています。
発生している問題・エラーメッセージ
{ "message": "Network Error", "name": "AxiosError", "config": { "transitional": { "silentJSONParsing": true, "forcedJSONParsing": true, "clarifyTimeoutError": false }, "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "maxBodyLength": -1, "env": { "FormData": null }, "headers": { "Accept": "application/json, text/plain, */*" }, "method": "get", "url": "http://localhost:8000" }, "code": "ERR_NETWORK", "status": null }
該当のソースコード
Test.js
1import React from "react"; 2import axios from "axios"; 3 4export default function Test() { 5 const onclick = () => { 6 axios.get("http://localhost:8000") 7 .then((res) => { 8 console.log(res) 9 }) 10 .catch((err) => { 11 console.log(err) 12 }) 13 } 14 return ( 15 <div onClick={onclick}>Click!</div> 16 ) 17}
main.js
1from fastapi import FastAPI 2 3app = FastAPI() 4 5@app.get("/") 6def read_root(): 7 return {"Hello": "World"}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。