服务端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;


namespace SoccerWeb
{
    /// <summary>
    /// ScoreHandler 的摘要说明
    /// </summary>
    public class ScoreHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=UTF-8"; // "text/plain";
            context.Response.AddHeader("Access-Control-Allow-Origin", "*");
            context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");

            JavaScriptSerializer json = new JavaScriptSerializer();
            Order p1 = new Order();
            p1.Name = "aa";
            p1.Age = 12;

            Order p2 = new Order();
            p2.Name = "bb";
            p2.Age = 12;

            string str = json.Serialize(new List<Order>() { p1, p2 });

            context.Response.Write(str);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  

客户端:

var winSize = null;

var XhrLayer = cc.Layer.extend({
    ctor:function () {
        if (!this._super()) {
            return false;
        }
        return true;

        this.onEnter();
    },

    onEnter: function() {
        this._super();
        var l = cc.LabelTTF.create("Get infos via XHR", "Thonburi", 16);
        this.addChild(l, 1);
        l.x = cc.director.getWinSize().width / 2;
        l.y = cc.director.getWinSize().height - 60;

        this.sendGetRequest();
        this.sendPostRequest();

        var l2 = cc.LabelTTF.create("Get infos via XHR2", "Thonburi", 16);
        this.addChild(l, 1);
        l2.x = cc.director.getWinSize().width / 2;
        l2.y = cc.director.getWinSize().height - 80;
    },

    sendGetRequest: function() {
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();
        var statusGetLabel = cc.LabelTTF.create("Status:", "Thonburi", 18);
        this.addChild(statusGetLabel, 1);
        statusGetLabel.x = cc.director.getWinSize().width / 2;
        statusGetLabel.y = cc.director.getWinSize().height - 100;
        statusGetLabel.setString("Status: Send Get Request to httpbin.org");
        
        xhr.open("GET", "http://172.18.65.100:8080/SoccerWeb/ScoreHandler.ashx");

        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var httpStatus = xhr.statusText;
                var response = xhr.responseText.substring(0, 50) + "...";
                var responseLabel = cc.LabelTTF.create("GET Response (50 chars): \n" + response, "Thonburi", 16);
                that.addChild(responseLabel, 1);
                responseLabel.anchorX = 0;
                responseLabel.anchorY = 1;
                responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;

                responseLabel.x = cc.director.getWinSize().width / 6;
                responseLabel.y = cc.director.getWinSize().height / 2;
                statusGetLabel.setString("Status: Got GET response! " + httpStatus);
            }
        };
        xhr.send();
    },

    sendPostRequest: function() {
        var that = this;
        var xhr = cc.loader.getXMLHttpRequest();
        var statusPostLabel = cc.LabelTTF.create("Status:", "Thonburi", 18);
        this.addChild(statusPostLabel, 1);

        statusPostLabel.x = cc.director.getWinSize().width / 2;

        statusPostLabel.y = cc.director.getWinSize().height - 140;
        statusPostLabel.setString("Status: Send Post Request to httpbin.org");

        
        xhr.open("POST", "http://172.18.65.100:8080/SoccerWeb/ScoreHandler.ashx");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var httpStatus = xhr.statusText;
                var response = xhr.responseText.substring(0, 50) + "...";
                var responseLabel = cc.LabelTTF.create("POST Response (50 chars):  \n" + response, "Thonburi", 16);
                that.addChild(responseLabel, 1);
                responseLabel.anchorX = 0;
                responseLabel.anchorY = 1;
                responseLabel.textAlign = cc.TEXT_ALIGNMENT_LEFT;

                responseLabel.x = cc.director.getWinSize().width / 2;
                responseLabel.y = cc.director.getWinSize().height / 2;
                statusPostLabel.setString("Status: Got POST response! " + httpStatus);
            }
            else
            {
                cc.log(xhr.statusText);
            }
        };
        xhr.send("test=ok");
    }
});

var XhrScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        this.addChild(new XhrLayer());
    }
});

  客户端打包后无法使用XMLHttpRequest,只能在网页版本中用

posted on 2014-04-19 14:46  凌度  阅读(1794)  评论(0编辑  收藏  举报