-
Notifications
You must be signed in to change notification settings - Fork 19
访问网络数据
li xianjing edited this page Sep 1, 2013
·
4 revisions
“画app吧”是使用HTML5开发app的,所以传统的ajax和websocket都是可以用的。
缺省情况下,由于js是不能进行跨域访问的,为了突破这一限制,在开发时,ajax请求自动通过drawapp8.com提供的代理访问网络数据。运行时则视情况而定,比如用phonegap编译成app在设备上运行不受跨域访问限制,生成webapp运行只能访问你自己服务器的数据(当然你可以加自己的代理)。
“画app吧”对ajax作了一下包装,大大简化了对网络数据的访问,比如我们开发一个词典应用,词典数据是从dict.cn上获取的:
var word = this.getWindow().findChildByName("ui-edit-word", true).getText();
var htmlview = this.getWindow().findChildByName("ui-simple-html", true);
if(!word) {
return;
}
function updateView(respContent) {
var div = document.createElement("div");
div.innerHTML = respContent;
basic = div.querySelector(".dict-basic-ul");
if(basic) {
console.log(basic.innerHTML);
htmlview.setText(basic.innerHTML);
}
else {
htmlview.setText("Not found.");
}
htmlview.postRedraw();
}
//下面是ajax请求
var rInfo = {};
rInfo.method = "GET";
rInfo.url = "http://dict.cn/" + word;
rInfo.noCache = true;
rInfo.onDone = function(result, xhr, respContent) {
if(xhr.status === 200) {
updateView(respContent);
}
}
httpDoRequest(rInfo);