【问题标题】:Spring React Chrome CORS error when accessed outside router在路由器外部访问时出现 Spring React Chrome CORS 错误
【发布时间】:2022-01-01 23:52:50
【问题描述】:

我使用 Chrome 作为我的默认浏览器。我在 Spring 后端和 React 前端中配置了跨源。

春天:@CrossOrigin(origins = "http://localhost:3000") 反应: const USER_API_BASE_URL = "http://localhost:8080/api/v1/users";

当我在我的电脑上工作时,一切都很好。我配置了 NOIP 并将我的路由器配置为将传入路由到我的开发 PC。当我使用外部地址启动会话时,出现 CORS 错误: ...

Access to XMLHttpRequest at 'http://localhost:8080/api/v1/users' from origin 'http://TESTURL.ddns.net:3000' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`.
xhr.js:187 GET http://localhost:8080/api/v1/users net::ERR_FAILED
dispatchXhrRequest @ xhr.js:187
xhrAdapter @ xhr.js:13
dispatchRequest @ dispatchRequest.js:53
request @ Axios.js:108
Axios.<computed> @ Axios.js:129
wrap @ bind.js:9
getUsers @ UserService.js:8
componentDidMount @ LoginComponent.jsx:19
commitLifeCycles @ react-dom.development.js:19816
commitLayoutEffects @ react-dom.development.js:22805
callCallback @ react-dom.development.js:188
invokeGuardedCallbackDev @ react-dom.development.js:237
invokeGuardedCallback @ react-dom.development.js:292
commitRootImpl @ react-dom.development.js:22543
unstable_runWithPriority @ scheduler.development.js:653
runWithPriority$1 @ react-dom.development.js:11039
commitRoot @ react-dom.development.js:22383
finishSyncRender @ react-dom.development.js:21809
performSyncWorkOnRoot @ react-dom.development.js:21795
scheduleUpdateOnFiber @ react-dom.development.js:21190
updateContainer @ react-dom.development.js:24375
(anonymous) @ react-dom.development.js:24760
unbatchedUpdates @ react-dom.development.js:21905
legacyRenderSubtreeIntoContainer @ react-dom.development.js:24759
render @ react-dom.development.js:24842
(anonymous) @ index.js:8
./src/index.js @ index.js:19
__webpack_require__ @ bootstrap:856
fn @ bootstrap:150
1 @ reportWebVitals.js:14
__webpack_require__ @ bootstrap:856
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.chunk.js:1
Show 4 more frames
createError.js:16 Uncaught (in promise) Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:99)

...

有没有办法解决这个问题?

【问题讨论】:

标签: reactjs spring google-chrome cors


【解决方案1】:

解决方案是 在 React 前端变化中

const USER_API_BASE_URL = "http://localhost:8080/api/v1/documenttype";
to
const USER_API_BASE_URL = "http://XXsystem.ddns.net:8080/api/v1/documenttype";

在 Spring 后端发生变化

@CrossOrigin("http://localhost:3000")
to
@CrossOrigin(origins = "http://XXsystem.ddns.net:3000")

有趣的是,'localhost 测试仍然有效! 两全其美。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 2020-10-12
    • 2020-06-30
    • 2018-01-19
    • 1970-01-01
    • 2018-03-19
    • 2017-02-14
    • 2023-03-26
    • 2017-04-24
    相关资源
    最近更新 更多