Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【cocos2d-js教程】cocos2d-js http网络请求大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文由qinning199原创,转载请注明:http://www.cocos2dx.net/post/216

研究了一下cocos2d-js的http网络请求,很简单

1、get请求,代码如下

var xhr = cc.loader.getXMLhttprequest();
        var statusGetLabel = new cc.LabelTTF("Status:","Thonburi",18);
        this.addChild(statusGetLabel,1);
        statusGetLabel.x = winSize.width / 2;
        statusGetLabel.y = winSize.height - 100;
        statusGetLabel.setString("Status: Send Get request to httpbin.org");
        //set arguments with <URL>?xxx=xxx&yyy=yyy
        xhr.open("GET","http://httpbin.org/get?show_env=1",truE);

        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 responseLabel = new cc.LabelTTF("GET Response (100 chars): \n" + response,16);
                that.addChild(responseLabel,1);
                responseLabel.anchorX = 0;
                responseLabel.anchorY = 1;
                responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;

                responseLabel.x = 10;
                responseLabel.y = winSize.height / 2;
                statusGetLabel.setString("Status: Got GET response! " + httpStatus);
            }
        };
        xhr.send();

2、post请求,代码如下
var xhr = cc.loader.getXMLhttprequest();
        var statusPostLabel = new cc.LabelTTF("Status:",18);
        this.addChild(statusPostLabel,1);

        statusPostLabel.x = winSize.width / 2;

        statusPostLabel.y = winSize.height - 140;
        statusPostLabel.setString("Status: Send Post request to httpbin.org with plain text");

        xhr.open("POST","http://httpbin.org/post");
        //set Content-type "text/plain;charset=UTF-8" to post plain text
        xhr.setrequestHeader("Content-Type","text/plain;charset=UTF-8");
        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 responseLabel = new cc.LabelTTF("POST Response (100 chars):  \n" + response,1);
                responseLabel.anchorX = 0;
                responseLabel.anchorY = 1;
                responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;

                responseLabel.x = winSize.width / 10 * 3;
                responseLabel.y = winSize.height / 2;
                statusPostLabel.setString("Status: Got POST response! " + httpStatus);
            }
        };
        xhr.send("plain text message");

大佬总结

以上是大佬教程为你收集整理的【cocos2d-js教程】cocos2d-js http网络请求全部内容,希望文章能够帮你解决【cocos2d-js教程】cocos2d-js http网络请求所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。