cocoscreater + java http连接

这篇博客探讨了如何在CocosCreator项目中使用Java进行HTTP连接,特别是在服务器端采用Spring Boot的情况下处理跨域问题。文章提到了@CrossOrigin注解的使用,并提供了两个关于Spring MVC CORS配置的参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值