Expedia API Cross Origin请求

时间:2013-09-08 09:48:30

标签: javascript api cors same-origin-policy

我正在使用Expedia API玩酒店搜索前端。我设置了一个在localhost:3000上运行的node.js服务器和一个简单的Backbone视图,用于输入位置和日期。

然而,当向Expedia提交请求时,我总是得到

XMLHttpRequest cannot load ... http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

这是我的提交代码,应该可以正常工作:

$.support.cors = true; // not really done at every request
var from = this.$el.find( "input[name=date-from_submit]" ).val(),
    to = this.$el.find( "input[name=date-to_submit]" ).val(),
    where = this.ui.place.val();

$.ajax({ 
    "url": "http://api.ean.com/ean-services/rs/hotel/v3/list?" + 
      "destinationString=" + where +
      "&cid=55505" + //test CID
      "&minorRev=20" +  
      "&arrivalDate=" + from + 
      "&departureDate=" + to +
      "&room1=2" + 
      "&apiKey=<apikey>",
    "dataType": "json",
    "accept": "application/json"
}).always( function( a, b, c ) {
    console.debug( a, b, c );
});

我从JSFiddle尝试了这个代码,看看他们是否只禁止localhost,但无济于事。同源策略错误。

现在我想知道:

  1. Expedia是否支持Cross Origin Requests?
  2. 也许我应该在他们网站的某个地方输入我选择的Origin?我没找到任何东西。
  3. 如果对1.的回答是“否”,那么开发人员应该如何使用他们的API构建产品?
  4. 我只是傻了吗?
  5. 提前致谢!

1 个答案:

答案 0 :(得分:1)

使用dataType:&#34; jsonp&#34;

$.ajax({
cors:true,
dataType:"jsonp",   
url:"API END POINT",
method:"GET",
success:function(data){
    console.log(data);
}

});