cocos2d js 请求网络

1,get请求


var xhr = cc.loader.getXMLHttpRequest();  

       var statusGetLabel = new cc.LabelTTF("Status:""Thonburi", 18);  

  1.         this.addChild(statusGetLabel, 1);  
  2.         statusGetLabel.x = winSize.width / 2;  
  3.         statusGetLabel.y = winSize.height - 100;  
  4.         statusGetLabel.setString("Status: Send Get Request to httpbin.org");  
  5.         //set arguments with <URL>?xxx=xxx&yyy=yyy  
  6.         xhr.open("GET""http://httpbin.org/get?show_env=1"true);  
  7.   
  8.         xhr.onreadystatechange = function () {  
  9.             if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
  10.                 var httpStatus = xhr.statusText;  
  11.                 var response = xhr.responseText.substring(0, 100) + "...";  
  12.                 var responseLabel = new cc.LabelTTF("GET Response (100 chars): \n" + response, "Thonburi", 16);  
  13.                 that.addChild(responseLabel, 1);  
  14.                 responseLabel.anchorX = 0;  
  15.                 responseLabel.anchorY = 1;  
  16.                 responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;  
  17.   
  18.                 responseLabel.x = 10;  
  19.                 responseLabel.y = winSize.height / 2;  
  20.                 statusGetLabel.setString("Status: Got GET response! " + httpStatus);  
  21.             }  
  22.         };  
  23.         xhr.send(); 
2.post请求

  1. var xhr = cc.loader.getXMLHttpRequest();  
  2.         var statusPostLabel = new cc.LabelTTF("Status:""Thonburi", 18);  
  3.         this.addChild(statusPostLabel, 1);  
  4.   
  5.         statusPostLabel.x = winSize.width / 2;  
  6.   
  7.         statusPostLabel.y = winSize.height - 140;  
  8.         statusPostLabel.setString("Status: Send Post Request to httpbin.org with plain text");  
  9.   
  10.         xhr.open("POST""http://httpbin.org/post");  
  11.         //set Content-type "text/plain;charset=UTF-8" to post plain text  
  12.         xhr.setRequestHeader("Content-Type","text/plain;charset=UTF-8");  
  13.         xhr.onreadystatechange = function () {  
  14.             if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
  15.                 var httpStatus = xhr.statusText;  
  16.                 var response = xhr.responseText.substring(0, 100) + "...";  
  17.                 var responseLabel = new cc.LabelTTF("POST Response (100 chars):  \n" + response, "Thonburi", 16);  
  18.                 that.addChild(responseLabel, 1);  
  19.                 responseLabel.anchorX = 0;  
  20.                 responseLabel.anchorY = 1;  
  21.                 responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;  
  22.   
  23.                 responseLabel.x = winSize.width / 10 * 3;  
  24.                 responseLabel.y = winSize.height / 2;  
  25.                 statusPostLabel.setString("Status: Got POST response! " + httpStatus);  
  26.             }  
  27.         };  
  28.         xhr.send("plain text message");


具体例子:

  1. var Http =  cc.Class.extend({  
  2.     m_inst : null, //实例  
  3.     url : "http://127.0.0.1:8080/request.php",  
  4.   
  5.     ctor : function(){  
  6.     },  
  7.   
  8.       /*  
  9.      * 网络请求之GET  
  10.      * url 请求的网络地址  
  11.      * callback 回调参数  
  12.      * */  
  13.     getWithUrl : function(url,callback){  
  14.         var xhr = cc.loader.getXMLHttpRequest();  
  15.         xhr.open("GET",url,true);  
  16.         xhr["onloadend"] = function () {  
  17.             if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
  18.                 err = false;  
  19.             }else{  
  20.                 err = true;  
  21.             }  
  22.             var response = xhr.responseText;  
  23.             callback(err,response);  
  24.         };  
  25.         xhr.send();  
  26.     },  
  27.    
  28.     /*  
  29.      * 网络请求之POST  
  30.      * url 请求的网络地址  
  31.      * params  请求参数  ("id=1&id=2&id=3")  
  32.      * callback 回调参数  
  33.     ['loadstart', 'abort', 'error', 'load', 'loadend', 'timeout']  
  34.     * */  
  35.     sendWithUrl : function(url, params, callback){  
  36.   
  37.         var xhr = cc.loader.getXMLHttpRequest();  
  38.         xhr.open("POST", url);  
  39.         xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");  
  40.         xhr["onloadend"] = function(){  
  41.   
  42.             var sc = -1  
  43.             if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {  
  44.                 sc = 0;  
  45.             }  
  46.   
  47.             var json = JSON.parse(xhr.responseText)  
  48.             var rc = parseInt(json["code"])  
  49.   
  50.             callback(sc, rc, json);  
  51.   
  52.             if(sc == 0 && (rc != 0) && RETCODE[rc + ""])  
  53.             {  
  54.                 Alert.getInst().show(RETCODE[rc + ""])  
  55.             }  
  56.             else if(sc != 0 || rc != 0 ){  
  57.                 Alert.getInst().show("sc: " + sc + " rc: " + rc)  
  58.             }  
  59.         }  
  60.        xhr.send(params);  
  61.     }  
  62. });  
  63.   
  64. //获取实例  
  65. Http.inst = function() {  
  66.     if (Http.m_inst == null) {  
  67.         Http.m_inst = new Http();  
  68.     }  
  69.     return Http.m_inst;  
  70. };  

使用方法

  1. Http.inst()->sendWithUrl("http://127.0.0.1:8080/request.php", "id=1&id=2&id=3", function(sc, rc, response){  
  2.   cc.log("返回数据" + response);  
  3. });  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值