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请求。我能够解决这个架构的问题。 –