CocosCreator JS/TS发送Get/Post请求的方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CocosCreator JS/TS发送Get/Post请求的方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4046字,纯文字阅读大概需要6分钟。
内容图文
![CocosCreator JS/TS发送Get/Post请求的方法](/upload/InfoBanner/zyjiaocheng/1033/331737974c7b4c08964488b601c10db4.jpg)
JS/TS发送Get/Post请求的方法
前言
使用CocosCreator制作前端小游戏,最后需要把数据以Json形式上传服务器。
使用XMLHttpRequest
主要分三步:
- 第一步:创建需要的对象,这里主要用到的是XMLHttpRequest,注意需要考虑早期的IE
- 第二步:连接和发送
- 第三步:接收
代码
Get请求
var httpRequest = new XMLHttpRequest(); //第一步:建立所需的对象
httpRequest.open('GET', 'url', true); //第二步:打开连接 将请求参数写在url中 ps:"./Ptest.php?name=test&nameone=testone"
httpRequest.send(); //第三步:发送请求 将请求参数写在URL中
/**
* 获取数据后的处理程序
*/
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText; //获取到json字符串,还需解析
console.log(json);
}
};
Post请求并设置请求头
var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
httpRequest.open('POST', 'url', true); //第二步:打开连接
httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)
httpRequest.send('name=teswe&ee=ef');//发送请求 将情头体写在send中
/**
* 获取数据后的处理程序
*/
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
var json = httpRequest.responseText;//获取到服务端返回的数据
console.log(json);
}
};
Post请求发送Json
var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
httpRequest.open('POST', 'url', true); //第二步:打开连接
/**
*发送json格式文件必须设置请求头 ;如下 -
*/
httpRequest.setRequestHeader("Content-type","application/json");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)
var obj = { name: 'zhansgan', age: 18 };
httpRequest.send(JSON.stringify(obj));//发送请求 将json写入send中
/**
* 获取数据后的处理程序
*/
httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
var json = httpRequest.responseText;//获取到服务端返回的数据
console.log(json);
}
};
CocosCreator HTTPUtil
最后分享一个小工具
export class HttpUtil {
/**
* 延迟多久没回复就返回False
*
* @type {number}
* @memberof DriveManager
*/
private static TimeOut:number=200;
/**
* GET请求
*
* @static
* @param {*} url
* @param {object} [params={}]
* @param {*} callback
* @memberof HttpUtil
*/
public static GET(url, params: object = {}, callback) {
let dataStr = '';
Object.keys(params).forEach(key => {
dataStr += key + '=' + encodeURIComponent(params[key]) + '&';
})
if (dataStr !== '') {
dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
url = url + '?' + dataStr;
}
// url = HttpUtil.baseUrl + url;
let xhr = cc.loader.getXMLHttpRequest();
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
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.timeout = this.TimeOut;
xhr.send();
}
/**
* POST请求
*
* @static
* @param {*} url
* @param {object} [param={}]
* @param {*} callback
* @memberof HttpUtil
*/
public static POST(url, param: object = {}, callback) {
// url = HttpUtil.baseUrl + url;
var xhr = cc.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");
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(dataStr);
}
}
内容总结
以上是互联网集市为您收集整理的CocosCreator JS/TS发送Get/Post请求的方法全部内容,希望文章能够帮你解决CocosCreator JS/TS发送Get/Post请求的方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。