0

我正在使用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);
};
4

1 回答 1

0

我认为这个错误可能是因为您在轮询会话时没有使用 api 密钥来调用,正如文档中所说的那样。试试这样:

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

无论如何,在Skyscanner 的常见问题解答中,Flights Live Pricing 服务不允许使用 CORS 版本,因此您可能必须从服务器端进行此调用。如果对您有帮助,我几天前在 Java 中发布了第一次调用的示例。

希望能帮助到你!

于 2016-08-28T17:12:30.630 回答