Я пытаюсь получить данные из моего бэкэнда. Я использую весеннюю загрузку и реагирую. Когда я пытался получить данные, я получил эту ошибку
Access to fetch at 'http://localhost:8080/getText' from origin 'http://localhost:3000' has been
blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource
with CORS disabled.
Так что после исследования я понял, что мне нужно настроить мой прокси. Поэтому я добавил прокси в мой package.json
файл, и теперь он выглядит так
{
"name": "web-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-scripts": "3.2.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:8080/",
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
И все же я получаю ту же ошибку. Это мой back-end и front-end, который я использую. Метод реагирования -
componentDidMount(){
fetch("http://localhost:8080/getText")
.then(response => response.json())
.then(data => console.log(data))
}
И пружинный контроллер -
@RestController
public class TextController {
@Autowired
private TextService textService;
@RequestMapping(path = "/getText", method = RequestMethod.GET)
public ResponseEntity<?> getAllText(){
List<String> result = textService.getAllText();
return new ResponseEntity(result, HttpStatus.OK);
}
}