在Titanium中创建HTTPClient

时间:2012-02-16 08:56:36

标签: titanium

我在Titanium中使用了Ti.Network.createHTTPClient,并且看到控件既不在onLoad也不在onError上。可能是什么原因?

 var loader = Titanium.Network.createHTTPClient();
       loader.onload = function() { 
      alert("Hello");   
        }  
      loader.onError = function(e) 
        alert("Error: " + e.error);
     }

4 个答案:

答案 0 :(得分:1)

添加这两行以使其正常工作!您没有发送请求,也没有发送URL

// add url in here 
loader.open("GET",'[URL HERE]'); 
// Send the request.
loader.send();

答案 1 :(得分:0)

var xhrSitelogin = Titanium.Network.createHTTPClient();
xhrSitelogin.open('POST', webservice_url);
xhrSitelogin.send({
method : "userlogin",
username : username,
password : password
});
xhrSitelogin.setTimeout(10000);

xhrSitelogin.onerror = function() {
showAlertBox('Service timed out. Please try again.');
//Hide Indicator
};
xhrSitelogin.onload = function() {

  alert(this.responseText);
   //RESPONSE RECEIVED
};

如果你认为它有用,请投票或标记最佳。

答案 2 :(得分:0)

嗨dosth尝试这个不确定它会工作如果它工作我会很高兴

var taskRequest = Titanium.Network.createHTTPClient();

    var api_url = 'http://myawesomeapi.heroku.com/users/' + 

Ti.App.Properties.getString(“userID”)+'/ tasks';

    taskRequest.onload = function() {

        var tasks = [];

        // code populating the tasks array

        alert(tasks);

        callback( tasks ); // invoke the callback
    }

    taskRequest.open('GET', api_url, false);

    taskRequest.setRequestHeader('Content-Type', 'application/json');

    taskRequest.send();

< ....>

答案 3 :(得分:0)

loader.open("POST/GET","URL");
loader.onload(response){
//get the response
console.log(this.responseText);

};
loader.send();
 Use this pattern.

如果您需要设置任何标头,请在loader.setRequestHeader("Content-Type", "application/json; charset=utf-8"); /之前open()onload()

之后使用send()