【发布时间】:2014-05-09 18:00:16
【问题描述】:
这是我目前如何使用钛进行 api 调用的示例:
var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload : function(e) {
Ti.API.info("Received text: " + this.responseText);
alert('success');
},
// function called when an error occurs, including a timeout
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
问题是这样操作,我只能在onload回调函数中访问对象。
例如,我不能这样做:
//snippet
var someObject;
onerror : function(e) {
someObject = this.responseText;
},
//end
function useObject(someObject){
alert(someObject);
}
使用 jquery AJAX 我可以做到这一点,像这样:
$.ajax({
type: "POST",
url: 'someurl',
data: param = "",
contentType: "application/json; charset=utf-8",
success: self.useObject,
error: errorFunc
});
收到响应后,将其传递给成功对象。
鉴于 Titanium 不使用 Jquery,我如何在 Titanium 中进行 equilent。
【问题讨论】:
标签: javascript jquery ajax titanium titanium-mobile