【cocos2d-js網路教程篇】cocos2d-js http網路請求

來源:互聯網
上載者:User

標籤:www   head   har   set   app   原因   add   XML   art   

前言

剛入手cocos2d-js,看到網上的JS的http網路請求,大部分都是錯的。原因在於,js-tests裡面的網路請求執行個體沒有給出載入完成事件。正確的載入完成事件如下:

var xhr = cc.loader.getXMLHttpRequest();          xhr.open("POST", url);          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");          xhr["onloadend"] = function(){  };

Http.js完整類

var Http =  cc.Class.extend({      m_inst : null, //執行個體      url : "http://127.0.0.1:8080/request.php",        ctor : function(){      },          /*       * 網路請求之GET       * url 請求的網路地址       * callback 回調參數       * */      getWithUrl : function(url,callback){          var xhr = cc.loader.getXMLHttpRequest();          xhr.open("GET",url,true);          xhr["onloadend"] = function () {              if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {                  err = false;              }else{                  err = true;              }              var response = xhr.responseText;              callback(err,response);          };          xhr.send();      },         /*       * 網路請求之POST       * url 請求的網路地址       * params  請求參數  ("id=1&id=2&id=3")       * callback 回調參數      [‘loadstart‘, ‘abort‘, ‘error‘, ‘load‘, ‘loadend‘, ‘timeout‘]      * */      sendWithUrl : function(url, params, callback){            var xhr = cc.loader.getXMLHttpRequest();          xhr.open("POST", url);          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");          xhr["onloadend"] = function(){                var sc = -1              if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) {                  sc = 0;              }                var json = JSON.parse(xhr.responseText)              var rc = parseInt(json["code"])                callback(sc, rc, json);                if(sc == 0 && (rc != 0) && RETCODE[rc + ""])              {                  Alert.getInst().show(RETCODE[rc + ""])              }              else if(sc != 0 || rc != 0 ){                  Alert.getInst().show("sc: " + sc + " rc: " + rc)              }          }         xhr.send(params);      }  });    //擷取執行個體  Http.inst = function() {      if (Http.m_inst == null) {          Http.m_inst = new Http();      }      return Http.m_inst;  };
使用方法
Http.inst()->sendWithUrl("http://127.0.0.1:8080/request.php", "id=1&id=2&id=3", function(sc, rc, response){    cc.log("返回資料" + response);  });

【cocos2d-js網路教程篇】cocos2d-js http網路請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.