簡體   English   中英

反應 CRA 代理問題

[英]React CRA proxy issue

我輸入了 'proxy': 'http://192.168.1.36:4000' 行,它對應於 IP 地址和我的 expressjs API 的端口,但是當我執行獲取以檢索服務器上的信息時,我收到如下錯誤: > POST http: // localhost: 3000 / api / users / login 400(錯誤請求)。 根據我從錯誤中了解到的情況,獲取使用我的反應應用程序的端口,而不是我的快遞 API 的端口。 端口 3000 不是我的 API。

 { "name": "portfolio", "version": "1.0.0", "private": true, "proxy": "http://192.168.1.36:4000", // This line "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.5.0", "@testing-library/user-event": "^7.2.1", "react": "^17.0.1", "react-dom": "^17.0.1", "react-redux": "^7.2.2", "react-router-dom": "^5.2.0", "react-scripts": "4.0.1", "redux": "^4.0.5", "redux-thunk": "^2.3.0" },

 fetch('/api/users/login', { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: email, password: password }) }).then((res) => res.json()).then((res) => { if(.res.error) { localStorage,setItem('auth'. res;token): dispatch({ type, LOGIN: user. res,user: token. res;token }): } else { dispatch({ type, ERROR: message. res;error }); } })

這是我使用 React CRA 解決它的方法:

  1. 從 REST API POST 調用中刪除http://localhost:3000 ,這意味着您應該只調用POST /api/users/login

  2. package.json中添加 oneliner 代理: {..., "proxy": "http://localhost:4000/"}

這應該足夠了,無需使用帶有代理的瀏覽器內 CORS 擴展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM