Network.js
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var Common = require('Common');
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
},
statics: {
//_count: 0,
/*
var txt = document.getElementById('txt');
//1.创建XMLHttpRequest对象
var xhr = null;
if(window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}else {
xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
//2.打开与服务器的链接
xhr.open('get',url,false);
//3.发送给服务器
xhr.send(null);
//4.响应就绪(同步请求)
var json = JSON.parse(xhr.responseText);
txt.innerHTML = json;
cc.log('其他程序');
*/
//ajax请求函数
ajax: function (strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oScope, oAdditionalData) {
var xlr = null;
var oResult = {};
if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc.
xlr = new XMLHttpRequest(); //Log.info('XMLHttpRequest');
} else if (window.ActiveXObject) {// code for IE6, IE5
xlr = new ActiveXObject("Microsoft.XMLHTTP"); //Log.info('ActiveXObject');
}
if (!xlr) {
cc.log("Your browser does not support XMLHTTP, using iframe instead..");
//退化到使用iframe 在cocos中无用 //这里两个函数名需要字符串输入
//iframeRequest(strMethod, strURL, astrContentType, oDataOrPostBody, onSuccess, onError, oAdditionalData)
return;
}
function onStateChange() {
if (xlr.readyState == 4) {// 4 = "loaded"
if (xlr.status == 200) {// 200 = OK
if (null != oScope) {
onSuccess.call(oScope, xlr.responseText, oAdditionalData);
} else {
onSuccess(xlr.responseText, oAdditionalData);
}
} else {
if (null != oScope) {
onError.call(oScope, xlr.responseText, oAdditionalData);
} else {
onError(xlr.responseText, oAdditionalData);
}
}
xlr = null;
}
}
strMethod = strMethod.toUpperCase();
var strDataOrPostBody = '';
if (strMethod == "GET") {
//以下这段可使用正则表达式改造
if (oDataOrPostBody) {
if (strURL.indexOf('?') > 0) { //有问号
if (strURL.indexOf('=') > strURL.indexOf('?') //并且有等号在问号右边
&& strURL.lastIndexOf('&') != strURL.length - 1) //最后一个不是&
{
strURL = strURL.concat('&');
} else { //光有问号没等号
cc.log('Unavailable Request URL!');
return;
}
} else {//没问号
strURL = strURL.concat('?');
}
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
}
strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
//strDataOrPostBody.TrimEnd('&');
} else {
cc.log('Unavailable Parameters!');
return;
}
}
strURL = strURL.concat(strDataOrPostBody);
}
xlr.onreadystatechange = onStateChange;
xlr.open(strMethod, strURL, true);
var isWWWFormURLEncoded = false;
if (astrContentType) {
for (var i = 0; i < astrContentType.length; i++) {
xlr.setRequestHeader('Content-type', astrContentType[i]);
if (astrContentType[i] == 'application/x-www-form-urlencoded') {
isWWWFormURLEncoded = true;
break;
}
}
}
//xlr.setRequestHeader('Connection', 'close');
switch (strMethod) {
case 'GET':
xlr.send(null);
break;
case 'POST':
if (isWWWFormURLEncoded) {
}
if (oDataOrPostBody) {
/*
if (isWWWFormURLEncoded) {
var fd=new FormData();
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
var aParameters1=oDataOrPostBody.split('&');
for (var i=0;i<aParameters1.length;i++){
var aPairs1=aParameters1.split('=');
if (aPairs1[0] && aPairs1[1]){
fd.append(aPairs1[0],aPairs1[1]);
}
}
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
fd.append(key,oDataOrPostBody[key]);
}
//strDataOrPostBody.TrimEnd('&');
//strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
} else {
console.log("Unavailable Parameters!");
return
}
xlr.send(fd);
}else{*/
if (typeof oDataOrPostBody == "string") {
strDataOrPostBody = oDataOrPostBody;
} else if (oDataOrPostBody instanceof Object) {
for (var key in oDataOrPostBody) {
strDataOrPostBody = strDataOrPostBody.concat(key).concat('=').concat(oDataOrPostBody[key]).concat('&');
}
//strDataOrPostBody.TrimEnd('&');
strDataOrPostBody = strDataOrPostBody.substring(0, strDataOrPostBody.length - 1);
} else {
oResult.businessCode = 'success';
oResult.description = 'Unavailable Parameters!';
return oResult;
}
xlr.send(strDataOrPostBody);
//}
} else {
xlr.send(null);
}
break;
}
},
loadImageInJSRuntime: function (strURL, onSuccess, onError, oScope) {
cc.loader.load(
{
url: strURL,
isCrossOrigin: false,
type: 'jpg' //防止跨域问题
},
function (err, texture) {
if (!err) {
if (null != oScope) {
onSuccess.call(oScope, texture);
} else {
onSuccess(texture);
}
} else {
if (null != oScope) {
onError.call(oScope, err);
} else {
onError(err);
}
}
}
);
},
loadImageInNativeRuntime: function (strURL, onSuccess, onError, oScope, node, strAnimationTag) {//留个钩子以便为图片加载做动画
let flag = false;
//这里使用cc.director.once而不是cc.director.on避免多次累加注册
cc.director.once('stop_render', function (event) { //注册并监听事件
cc.log("Network(diapatchEvent)-------------------------->");
flag = true;
});
try {
cc.loader.load(strURL, function (err, texture) {
if (null != oScope) {
// let flag = false;
// if (Common.g_strCurrentSceneName && cc.director.getScene() && Common.g_strCurrentSceneName == cc.director.getScene().name) {
// flag = true;
// }
if (!flag) {//如果已经切换场景就不做下面这件事情
try {
onSuccess.call(oScope, texture);
} catch (err) {
cc.log(err);
}
}
} else {
onSuccess(texture);
}
// cc.log('Should load a texture from external url: ' + (texture instanceof cc.Texture2D));
});
} catch (err) {
cc.log(err);
}
},
},
// called every frame, uncomment this function to activate update callback
// update: function (dt) {
// },
});