Util.fetch = function(path,callback) {
if (callback && typeof callback != 'function')
callback = null;
if (Util.IS_NODE) {
var fs = require("fs");
if (callback) {
fs.readFile(path,function(err,data) {
if (err)
callback(null);
else
callback(""+data);
});
} else
try {
return fs.readFileSync(path);
} catch (e) {
return null;
}
} else {
if(cc.sys.isNative) {
//Native版本需要特殊处理下ProtoBuf的读取方式
var msg = jsb.fileUtils.getStringFromFile(path);
if(callback){
callback(msg);
}else{
return msg;
}
}else{
var xhr = Util.XHR();
xhr.open('GET',path,callback ? true : false);
// xhr.setRequestHeader('User-Agent','XMLHTTP/1.0');
xhr.setRequestHeader('Accept','text/plain');
if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
if (callback) {
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
callback(xhr.responseText);
else
callback(null);
};
if (xhr.readyState == 4)
return;
xhr.send(null);
} else {
xhr.send(null);
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
return xhr.responseText;
return null;
}
}
}
};
原文链接:https://www.f2er.com/cocos2dx/339209.html