cocosCreator 网络请求接口(json和表单两种)

import { Component, loader } from "cc";


const url_http = "https://xqapi.haoyingsheng.top/api";

export default class HttpUtil  {
    /**

    * 请求协议的方法

    * @param path 请求接口的路径

    * @param params 参数

    * @param callBack 回调函数

    */

     public static get(path, params, callBack) {

        var requestUrl = url_http + path;

        var xhr = loader.getXMLHttpRequest();

        // var data=self.paramData(params);

        var data = params;

        let param = '?';

        for (var key in data) {

            var paramStr = key + "=" + data[key];

            if (param == "") {

                param += paramStr;

            } else {

                param += "&" + paramStr;

            }

        }

        xhr.open("GET", requestUrl + param);

        xhr.timeout = 5000;//

        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        xhr.onreadystatechange = function () {

            if (xhr.readyState === 4 && xhr.status == 200) {

                var respone = xhr.responseText;

                console.log('响应参数')

                console.log(respone)

                callBack(JSON.parse(respone));

            }

        };

        xhr.send();

    }

    public static POST(url, param: object = {}, callback) {
        url = url_http+url;
        var xhr = loader.getXMLHttpRequest();
        let dataStr = '';
        Object.keys(param).forEach(key => {
            dataStr += key + '=' + encodeURIComponent(param[key]) + '&';
        })
        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
        }
        xhr.open("POST", url, true);
        // 第一种表单提交  
        // xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        // 第二种  json提交
        xhr.setRequestHeader("Content-Type", "application/JSON");
       
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                let response = xhr.responseText;
                if (xhr.status >= 200 && xhr.status < 300) {
                    let httpStatus = xhr.statusText;
                    // callback(true, JSON.parse(response));
                    callback(true, response);

                } else {
                    callback(false, response);
                }
            }
        };
        // 第一种表单提交 和上面配合使用 
        // xhr.send(JSON.stringify(dataStr));
        //  第二种  json提交  和上面配合使用
        xhr.send(JSON.stringify(param));
    }

}

function ccclass(arg0: string) {
    throw new Error("Function not implemented.");
}

自己做的小游戏感兴趣可以扫码体验,可以交流游戏

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞飞翼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值