我正在使用SkyScanner API按航班详细信息获取实时价格。
正如文档所说。我创建了实时定价服务会话。可以通过向 api 发布请求来创建它,然后它通过使用这个 SessionKey 和 apiKey 提供 SessionKey 我可以检索数据。我可以看到我在成功方法中使用的带有 getResponseHeader("Location") 的 sessionKey。然后我将它提交给一个全局变量 urlSession,我稍后在另一个 http get 请求中将其用作 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);
};