0

我正在與SkyScanner API合作以獲取按航班詳細信息顯示的實時價格。如何在get方法中使用post方法中的getResponseHeader?

正如文檔所述。我創建了實時定價服務會議。可以通過向api發送請求來創建,然後通過使用這個SessionKey和apiKey來提供SessionKey,我可以回顧這些數據。我可以在成功方法中看到使用getResponseHeader(「Location」)的sessionKey。我將它提交給一個全局變量urlSession,我稍後在另一個http獲取請求中使用它作爲url。 我可以在警報中看到SessionKey,但我嘗試在get方法中使用它時出現未定義的錯誤。我不確定它是CORS問題,還是語法。

var hrpost = Ti.Network.createHTTPClient(); 
// post_params 
    var post_params = { 
     "apiKey" : {apiKey}, 
     "Country" : "US", 
    "Currency" : "USD", 
    "Locale" : "en-GB", 
    "Adults" : 1, 
    "Children" : 0, 
    "Infants" : 0, 
    "OriginPlace" : "16216", 
    "DestinationPlace" : "1111", 
    "OutboundDate" : "2016-09-23", 
    "InboundDate" : "2016-09-30", 
    "LocationSchema" : "Default", 
    "CabinClass" : "Economy", 
    "GroupPricing" : true 

    }; 
    var strMyObj = JSON.stringify(post_params); 
    //Here I set the webservice address and method 
hrpost.open('POST', "http://partners.api.skyscanner.net/apiservices/pricing/v1.0"); 
//Set the headers 
    hrpost.setRequestHeader("Accept", "application/json"); 
hrpost.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
hrpost.send(post_params); 

hrpost.onload = function(){ 

    alert('Posted successfully'); 
    urlsessionKey = hrpost.getResponseHeader('Location'); 
    alert(urlsessionKey); 

} 
hrpost.onerror = function() { 
alert('request didnt posted'); 
var rejection = { 
      status: hrpost.status, 
      statusText: hrpost.statusText, 


     }; 
     alert(rejection); 
}; 



//HTTP request get method to send the sessionKey and retrieve data 
var xmlHttp = Ti.Network.createHTTPClient(); 



var post_params = { 
     "apiKey" : {apiKey}, 
     "Country" : "US", 
    "Currency" : "USD", 
    "Locale" : "en-GB", 
    "Adults" : 1, 
    "Children" : 0, 
    "Infants" : 0, 
    "OriginPlace" : "16216", 
    "DestinationPlace" : "1111", 
    "OutboundDate" : "2016-09-23", 
    "InboundDate" : "2016-09-30", 
    "LocationSchema" : "Default", 
    "CabinClass" : "Economy", 
    "GroupPricing" : true 

    }; 
//here the error occurs when I use the sessionKey I stored in the global var urlsessionKey 
xmlHttp.open('GET', urlsessionKey); 

    xmlHttp.setRequestHeader("Accept", "application/json"); 
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 


xmlHttp.send(post_params); 

xmlHttp.onload = function(e){ 

    alert('Get successfully'); 
} 
xmlHttp.onerror = function() { 
alert('request didnt posted'); 
var rejection = { 
      status: xmlHttp.status, 
      statusText: xmlHttp.statusText, 

     }; 
     alert(rejection); 
}; 

回答

0

我想也許是因爲你沒有使用API​​密鑰您的來電錯誤是當投票會議,因爲它says的文件中。試着這樣說:

var urlsessionKey = hrpost.getResponseHeader('Location') + "?apiKey=" + {apiKey}; 

反正在FAQ of Skyscanner看來,機票價格實時服務不允許CORS版本,所以也許你需要做的,從你的服務器端解決此通話。我幾天前發佈了我的example in Java的第一個電話,如果它對你有幫助。

希望它有幫助!

+0

感謝您的回答。我能夠通過hrpost.getResponseHeader('Location')獲取sessionKey,而不用追加apiKey。我的代碼的問題是我正在做的調用順序,我將發佈的http請求設置爲false異步。我在post請求的onload方法中發出了我的get請求。我能夠解決這個架構的問題。 –