【问题标题】:create HTTPClient in Titanium在 Titanium 中创建 HTTPClient
【发布时间】:2012-02-16 08:56:35
【问题描述】:

我在 Titanium 中使用了 Ti.Network.createHTTPClient,发现控件既不在 onLoad 内部也不在 onError 内部。可能是什么原因?

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

【问题讨论】:

标签: titanium


【解决方案1】:

添加这两行使其工作!你没有发送请求,也没有发送 URL

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

【讨论】:

    【解决方案2】:
    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
    };
    

    如果您认为它有帮助,请投票或标记为最佳。

    【讨论】:

      【解决方案3】:

      您好,请尝试一下,我不确定它是否会起作用,我会很高兴

      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();
      

      <....>

      【讨论】:

        【解决方案4】:
        loader.open("POST/GET","URL");
        loader.onload(response){
        //get the response
        console.log(this.responseText);
        
        };
        loader.send();
         Use this pattern.
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-02
          相关资源
          最近更新 更多