2015-10-13 41 views
0

我有一個網站,其中包含供應商頁面。在網頁上,當用戶點擊特定公司時,與供應商相關的所有數據都會返回,但是當供應商api被調用時,它會傳遞一個變量名稱x-access-token,這通常是用戶登錄時返回的數據。請看下圖。鈦API調用 - HTTP錯誤

enter image description here

現在我想使用相同的API在我的Titanium應用程序,並得到特別是公司所有的供應商。以下提到的代碼已由我自己編寫

var url = "http://www.www.com/vendor?company=demo"; 

var xhr = Ti.Network.createHTTPClient({ 
    onload: function(e) { 
     // function called in readyState DONE (4) 
     Ti.API.info('onload called, readyState = '+this.readyState); 
     Ti.API.info("Status " + xhr.status); 
    }, 
    onerror: function(e) { 
     // function called in readyState DONE (4) 
     Ti.API.info('onerror called, readyState = '+this.readyState); 
     Ti.API.info("Status " + xhr.status); 
     Ti.API.info('ERROR - '+e.error); 
    }, 
    ondatastream: function(e) { 
     // function called as data is downloaded 
     Ti.API.info('ondatastream called, readyState = '+this.readyState); 
     Ti.API.info("Status " + xhr.status); 
    }, 
    onsendstream: function(e) { 
     // function called as data is uploaded 
     Ti.API.info('onsendstream called, readyState = '+this.readyState); 
     Ti.API.info("Status " + xhr.status); 
    }, 
    onreadystatechange: function(e) { 
     switch(this.readyState) { 
      case 0: 
       // after HTTPClient declared, prior to open() 
       // though Ti won't actually report on this readyState 
       Ti.API.info('case 0, readyState = '+this.readyState); 
       break; 
      case 1: 
       // open() has been called, now is the time to set headers 
       Ti.API.info('case 1, readyState = '+this.readyState); 
       break; 
      case 2: 
       // headers received, xhr.status should be available now 
       Ti.API.info('case 2, readyState = '+this.readyState); 
       break; 
      case 3: 
       // data is being received, onsendstream/ondatastream being called now 
       Ti.API.info('case 3, readyState = '+this.readyState); 
       break; 
      case 4: 
       // done, onload or onerror should be called now 
       Ti.API.info('case 4, readyState = '+this.readyState); 
       break; 
     } 
    }, 
    timeout:7000 /* in milliseconds */ 
}); 

xhr.open('GET', url); 
xhr.setRequestHeader('x-access-token', userToken); 
xhr.send(); 

以下是輸出相同。

case 1, readyState = 1 
case 3, readyState = 3 
case 4, readyState = 4 
ondatastream called, readyState = 4 
Status 403 
case 4, readyState = 4 
onsendstream called, readyState = 4 
Status 403 
ondatastream called, readyState = 4 
Status 403 
onerror called, readyState = 4 
Status 403 
ERROR - HTTP error 

我得到HTTP錯誤。我是以錯誤的方式爲x-access-token設置標題,還是爲了執行相同的操作而存在任何替代方法?任何人都可以幫助我解決這個問題嗎?任何有關同樣的建議將不勝感激。

回答

1

這似乎沒問題。將網址替換爲http://requestb.in,以便您可以看到確切的請求。

+0

感謝您的寶貴意見。我試着改變從requestbin創建的url。在那裏我可以得到請求標題。 X-Access-Token:「eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhZG1pbiIsImV4cCI6MTQ3NjM1NTY2NjEwMn0.rVEeltZxOY5wFIskL0J95vltv-tOHeVsSUVMopVQ8pg」。但是我已經通過了請求標頭x-access-token。我還注意到的另一件事是,雖然從網站API調用完成,它會給我回復x-access-token:eyJ0eXAiOiJIUKI1NiJ9.eyJpc3MiOiJhZG1pbiIsImV4cCI6MTQ3NjM1NjM1MjAxOX0.nm6lZN9jUnFBfe_0ZeI0JbjquljOu4uVbar1ar1ovpU。它會有所作爲嗎? ? –

0

解決了這個問題,方法是在字符串化後檢查傳遞查詢字符串,即company=demo。以及在setRequestHeader中傳遞的userToken,如下所示。 xhr.setRequestHeader('x-access-token', userToken);作爲字符串傳遞,因此我在http://requestb.in中獲得X-Access-Token : "randomtoken",而當從我的網站調用webservice時,它簡單地通過顯示X-Access-Token : randomtoken如此轉換的令牌類型爲對象,同時存儲以及提取,即​​。它可能對某個人有幫助。