我有一个带有@Controller的SpringBoot应用程序,它接受对localhost:8080 / all的getRequest并返回JSON . 我已在浏览器中输入此域并查看返回的JSON .
但是,当我尝试使用来自我的React应用程序(本地运行)的Axios get请求来访问此URL时,而不是返回JSON数据,它会返回以下错误:
Network Error at createError (http://localhost:3000/static/js/bundle.js:1634:15) at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:1170:14)
我检查了Chrome的网络标签中的get请求,它没有返回预览或响应的任何内容,但确实指出:
Request URL: https://localhost:8080/all
Referrer Policy: no-referrer-when-downgrade
在 Headers 部分 .
这两个应用程序都在本地运行,当我尝试不同的axios get请求时(对于不同的API),它会成功返回JSON . 如何从Spring Boot应用程序成功返回JSON?
API调用的React设置是:
componentDidMount() {
axios.get('https://localhost:8080/all')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
Spring Boot应用程序中的控制器操作是:
@RestController
public class UserController {
@Autowired
UserJDBCDao dao;
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return dao.findAll();
}
}
1 回答
检查Spring Boot错误日志后,它指出:
所以我将https更改为http并且请求有效 . 我不确定这是否是最安全的方式,所以其他人可能有更好的解决方案 .