【发布时间】:2019-01-09 11:09:33
【问题描述】:
我正在尝试学习如何将 React Native 应用程序(目前也使用 expo)与 Spring Boot 服务器连接起来。
我在网上看到有关此错误消息的类似问题,并回答说这是由 URL 中的错误引起的问题。是这样吗?我看到的另一个响应是“Ünrecognized token'
对于我的应用程序“exp://172.20.10.6:19000”是应用程序端口(至少,我相信它是这样的)。
但我得到了 JSON 解析错误。
"JSON 解析错误:无法识别的令牌 '
class GamesCollection extends Component {
constructor(props){
super(props);
this.state = {
loading: true,
error: false,
games: [],
}
}
componentDidMount = async () => {
try {
let response = await fetch('http://172.20.10.6:19000/games',{
headers:{
Accept:'application/json',
'Content-Type':'application/json',
}
})
let games = await response.json()
this.setState({loading: false, games})
} catch (e) {
console.log(e);
this.setState({loading: false, error: true})
}
}
server in Java Spring Boot:
@RestController
@RequestMapping("/")
public class GameController {
private final GameRepository repository;
GameController(GameRepository repository) {
this.repository = repository;
}
//Aggregate root
@GetMapping("/games")
@CrossOrigin(origins = "")
public List<Game> all() {
return repository.findAll();
}
【问题讨论】:
-
尝试使用新的 Headers() 构造函数构造标题。
-
您的代码没有问题。您是否在 SpringBoot-Server 上启用了 CORS?
标签: spring-boot expo react-native-ios