客户端js:
var xhr = new XMLHttpRequest();
var url ="http://192.168.124.6:8080/get1?ids=1";
xhr.onreadystatechange = function () { //注册回调的方法,发送成功后执行
if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {
// var httpStatus = xhr.statusText;
// var response = xhr.responseText//.substring(0, 100) + "...";
var response = xhr.response;
if(response == ""){
self.bestScoreLabel.getComponent(cc.Label).string = "最高分数: " + self.bestScore;
}else{
self.bestScore = JSON.parse(response).score;
self.bestScoreLabel.getComponent(cc.Label).string = "最高分数: " + self.bestScore;
}
// user=JSON.parse(response).datas;
// cc.log(user);
// cc.log("GET Response (100 chars): \n" + response);
// cc.log("Status: Got GET response! " + httpStatus);
}
};
xhr.open("GET",url,true);
xhr.send();
服务器 spring boot
@Controller
@EnableAutoConfiguration
public class SampleController {
@Autowired
Jumpservice jumpService;
@CrossOrigin(origins = {"http://localhost:7456", "null"})
// @RequestMapping("/")
@RequestMapping(value = "/get1", method = RequestMethod.GET)
@ResponseBody
Map<String,Integer> findInfo(@RequestParam(value = "ids") String ids) {
return jumpService.findData(ids);
}
@CrossOrigin(origins = {"http://localhost:7456", "null"})
// @RequestMapping("/")
@RequestMapping(value = "/get2", method = RequestMethod.GET)
@ResponseBody
Boolean getInfo(@RequestParam(value = "ids") String ids,@RequestParam(value = "score") Integer score) {
return jumpService.setData(ids,score);
}
}
注意
@CrossOrigin(origins = {"http://localhost:7456", "null"})
关于跨域连接问题查看参考的两个链接。
参考:
1.https://blog.coding.net/blog/spring-mvc-cors
2.ttps://juejin.im/entry/59b8fb276fb9a00a42474a6f