最近搜索

发送get 请求 和post请求

浏览:561
管理员 2021-09-16 03:06




 
cc.Class({
    extends: cc.Component,

    properties: {
    },
    onLoad() { 
        this.sendHttpGet();
    },

    start() {

    },
    sendHttpGet : function(){
        var self = this;
        var request = cc.loader.getXMLHttpRequest();
        var url = "http://localhost:8080/user/get";
        request.open("GET",url,true);
        request.onreadystatechange = function(){
            if(request.readyState == 4 && (request.status >= 200 && request.status < 300)){
                var response = request.responseText;
                console.log("sendHttpGet:");
                console.log(response);
                var responseJson = JSON.parse(response)
                console.log(responseJson);
                console.log(responseJson.msg);
                //self.httpGetRes.string = responseJson["headers"]["Accept-Encoding"];
            }
        }
        request.send();
    },


    sendHttpPost : function(param){
        var self = this;
        var request = cc.loader.getXMLHttpRequest();
        var url = "http://localhost/api/cocos/post";
        request.open("POST", url,true);
        request.setRequestHeader("Content-Type","text/plain;charset=UTF-8");
        request.onreadystatechange = function(){
            if(request.readyState == 4 && (request.status >= 200 && request.status < 300)){
                var response = request.responseText;
                console.log("sendHttpPost:");
                console.log(response);
                var responseJson = JSON.parse(response)
                //self.httpPostRes.string = responseJson["data"];
            }
        }
        //这个参数 尽量使用json格式的。后台要解析方便 。
        var t1 = {};
        t1.name="12";
        t1.name=500;
        t1.f=true;
        var jsonstr = JSON.stringify(t1);
        console.log(jsonstr);
        request.send(jsonstr);//后台接受数据流  解析json
        //request.send(new Uint8Array([1,2,3,4,5]));
    },
    

    sendHttpTimeOut : function(){
        var self = this;
        var request = cc.loader.getXMLHttpRequest();
        var url = "https://192.168.22.222";
        request.open("GET",url,true);
        setTimeout(function(){  
            console.log("网络请求超时");
            request.abort(); //断开连接 
            self.httpTimeoutRes.string = "网络请求超时";
        }, 5000);  
        request.send();
    },

    // update (dt) {},
});




post后台代码


	
	
	@CrossOrigin
	@ResponseBody
	@RequestMapping("/api/cocos/post")
	public JSONObject post(HttpServletRequest request)throws Exception {
		JSONObject result = new JSONObject();
		 StringBuffer sb = new StringBuffer();
	    InputStream is = request.getInputStream();
	    InputStreamReader isr = new InputStreamReader(is);
	    BufferedReader br = new BufferedReader(isr);
	    String s = "";
	    while ((s = br.readLine()) != null) {
	        sb.append(s);
	    }
	    String result1 = sb.toString();
	    System.out.println(result1);

		result.put("msg","123456adbd我爱北京");
		result.put("user", userDao.findAll());
		return result;
	}
	



image.png



image.png

联系站长

站长微信:xiaomao0055

站长QQ:14496453