@Async 造成 request.getHeader()
时间: 2023-10-23 21:14:10 浏览: 44
@Async注解可以使方法异步执行,但是在异步线程中使用了request对象,而request对象是有生命周期的、复用的,异步线程未处理request的情况下,会被回收复用,导致下一次使用解析不出参数,从而造成request.getHeader()等方法返回null的情况。为了解决这个问题,可以使用Callable或DeferredResult等方式来处理异步请求,或者在异步线程中使用ThreadLocal来保存request对象,避免被回收复用。
相关问题
xhr.js:160 POST http://localhost:9998/user 500 dispatchXhrRequest @ xhr.js:160 xhrAdapter @ xhr.js:11 dispatchRequest @ dispatchRequest.js:59 Promise.then (async) request @ Axios.js:51 wrap @ bind.js:9 addUser @ userManage.js:22 eval @ user.vue:190 eval @ element-ui.common.js:22992 eval @ element-ui.common.js:23510 complete @ index.js:89 eval @ index.js:225 next @ util.js:148 next @ util.js:116 cb @ index.js:173 type @ type.js:17 eval @ index.js:216 next @ util.js:114 cb @ index.js:173 required @ required.js:8 eval @ index.js:216 next @ util.js:114 asyncSerialArray @ util.js:120 eval @ util.js:154 asyncMap @ util.js:151 validate @ index.js:141 validate @ element-ui.common.js:23506 eval @ element-ui.common.js:22986 validate @ element-ui.common.js:22985 saveUser @ user.vue:187 invokeWithErrorHandling @ vue.runtime.esm.js:1854 invoker @ vue.runtime.esm.js:2179 invokeWithErrorHandling @ vue.runtime.esm.js:1854 Vue.$emit @ vue.runtime.esm.js:3882 handleClick @ element-ui.common.js:9417 invokeWithErrorHandling @ vue.runtime.esm.js:1854 invoker @ vue.runtime.esm.js:2179 original._wrapper @ vue.runtime.esm.js:6911 request.js:75 errError: Request failed with status code 500
根据提供的错误信息,可以看出发生了一个 HTTP500 错误。HTTP 500 错误通常表示服务器内部错误,表示服务器在处理请求时发生了一些问题。
在你的情况下,出现了一个 POST 请求到 `http://localhost:9998/user` 的错误,返回了一个 HTTP 500 错误。这意味着在发送这个请求时,服务器遇到了一些问题,并且无法正确处理请求。
要解决这个问题,你可以进行以下步骤:
1. 检查服务器日志:查看服务器的日志文件,以了解更多关于发生了什么问题的详细信息。通常,服务器日志可以提供有关错误的更多上下文和堆栈跟踪。
2. 检查请求的数据:确保在发送请求时提供了正确的数据。检查 `addUser` 方法中的 `user` 参数是否包含了必要的数据,并且格式正确。
3. 检查服务器代码:检查服务器端代码,特别是处理 `POST /user` 请求的逻辑。确保代码没有错误或异常,以及逻辑是否正确处理请求。
4. 确保服务器正在运行:确认服务器正在运行,并且监听 `http://localhost:9998` 这个地址。如果服务器没有正常启动或没有监听正确的端口,那么发送请求会失败。
通过以上步骤来检查和调试可能会帮助你找到导致 HTTP 500 错误的原因,并解决问题。
Access to XMLHttpRequest at 'http://localhost:8080/springprojecttraining/api/updateUser' from origin 'http://localhost:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. xhr.js:160 POST http://localhost:8080/springprojecttraining/api/updateUser net::ERR_FAILED 200 dispatchXhrRequest @ xhr.js:160 xhrAdapter @ xhr.js:11 dispatchRequest @ dispatchRequest.js:59 Promise.then (async) request @ Axios.js:51 Axios.<computed> @ Axios.js:71 wrap @ bind.js:9 submitForm @ user.vue:346 click @ user.vue:407 invokeWithErrorHandling @ vue.esm.js:3735 invoker @ vue.esm.js:1552 invokeWithErrorHandling @ vue.esm.js:3735 Vue.$emit @ vue.esm.js:2821 handleClick @ element-ui.common.js:9465 invokeWithErrorHandling @ vue.esm.js:3735 invoker @ vue.esm.js:1552 original_1._wrapper @ vue.esm.js:8013 createError.js:16 Uncaught (in promise) Error: Network Error at createError (createError.js:16:1) at XMLHttpRequest.handleError (xhr.js:69:1)为什么报错 如何解决呢
这是因为浏览器实施了同源策略(Same-Origin Policy)限制,导致从一个源访问另一个源时会被拒绝,特别是在前端使用JavaScript发起Ajax请求时。在这种情况下,需要进行跨域资源共享(CORS)配置。
解决方法如下:
1. 在服务器端配置CORS,允许指定的源或所有源访问:
- 允许指定源访问:
在服务器端的响应中添加Access-Control-Allow-Origin头,指定允许访问的源,例如:
```
Access-Control-Allow-Origin: http://localhost:8081
```
- 允许所有源访问:
在服务器端的响应中添加Access-Control-Allow-Origin头,指定允许所有源访问,例如:
```
Access-Control-Allow-Origin: *
```
2. 在前端使用代理服务器(Proxy)转发请求,以绕过浏览器的同源策略限制。
例如,使用Vue.js,可以在vue.config.js中添加以下配置:
```
module.exports = {
devServer: {
proxy: {
'/springprojecttraining': {
target: 'http://localhost:8080',
changeOrigin: true
}
}
}
}
```
这样,在前端使用axios等工具时,只需要将请求的URL改为代理服务器地址,例如:
```
axios.post('/springprojecttraining/api/updateUser', data)
```
这样就可以解决跨域问题了。