SkyScanner APIを使用して、フライトの詳細でライブ料金を取得しています。
ドキュメントが言うように。Live Pricing Service セッションを作成しました。これは、リクエストを api に送信することで作成でき、この SessionKey と apiKey を使用して SessionKey を提供し、データを取得できます。成功メソッドで使用する getResponseHeader("Location") で sessionKey を確認できます。そして、後で別の http get リクエストで URL として使用するグローバル変数 urlSession に送信します。アラートで 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);
};