Reactjs:TypeError:未能执行';获取';在';窗口';:无效名称
关于主题错误,我不知道原因,在谷歌做了很多研究,但是 答案对我的案子不起作用。服务在Springboot中,它与postman一起给出了正确的结果,而我的react应用程序失败了。任何帮助都将不胜感激。查看我的代码Reactjs:TypeError:未能执行';获取';在';窗口';:无效名称,reactjs,spring-boot,typeerror,Reactjs,Spring Boot,Typeerror,关于主题错误,我不知道原因,在谷歌做了很多研究,但是 答案对我的案子不起作用。服务在Springboot中,它与postman一起给出了正确的结果,而我的react应用程序失败了。任何帮助都将不胜感激。查看我的代码 getSites = () => { fetch("http://localhost:8080/xyz/sites", { "method": "GET", "headers": {
getSites = () => {
fetch("http://localhost:8080/xyz/sites", {
"method": "GET",
"headers": {
'Content-Type':'application/json',
'Access-Control-Allow-Origin:':'http://localhost:8080',
'accept':'application/json',
'content-type':'application/json'
}
})
.then(response => response.json())
.then(response => {
this.setState({
sites: response
})
})
.catch(err => {
console.log(err);
});
return this.state.sites;
}
postman中的json响应是
[
{
"parkDimKey": "12345",
"parkName": "Site 1"
},
{
"parkDimKey": "67890",
"parkName": "Site 2"
}
]
Spring启动服务是
@RestController
public class SiteDataController {
@Autowired
SiteDataDao siteDataDao;
@RequestMapping("/sites")
public List<SiteData> getSiteData(){
return siteDataDao.getSiteData();
}
}
我有一个类似的问题,结果是一个简单的打字错误。我从标题名中删除了一个冒号符号,它成功了:
'Access-Control-Allow-Origin:':'http://localhost:8080“,
'Access-Control-Allow-Origin':'http://localhost:8080“,
我有一个类似的问题,结果是一个简单的打字错误。我从标题名中删除了一个冒号符号,它成功了:
'Access-Control-Allow-Origin:':'http://localhost:8080“,
'Access-Control-Allow-Origin':'http://localhost:8080“,
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/xyz/sites").allowedOrigins("http://localhost:8080");
}
};
}
}