【问题标题】:Making a cross domain ajax request to a linkedIn api endpoint向linkedIn api端点发出跨域ajax请求
【发布时间】:2016-07-18 13:17:51
【问题描述】:

我正在尝试通过向linkedIn api 端点发出GET 请求来获取网站文章的分享数:

https://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json

这是我的代码:

$.ajax({
    url: 'http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json',
    type: 'GET',
    processData: false,
    crossDomain: true,
    contentType: "application/json",
    jsonp: false,
    success: function(data) { alert('succeeded'); },
    error: function() { alert('Failed!'); },
});

当我运行它时,我得到:

XMLHttpRequest cannot load http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://aranca.craft.dev' is therefore not allowed access.`

我做错了什么?如何访问 sharedcount 数据?

【问题讨论】:

    标签: jquery ajax cors


    【解决方案1】:

    您可以使用dataType: 'jsonp' 来解决这个CORS 问题,例如:

    $.ajax({
      url: 'https://www.linkedin.com/countserv/count/share?url=http://stylehatch.co',
      type: 'GET',
      contentType: "application/json",
      dataType: 'jsonp',
      success: function(data) {
        console.log('Count: ' + data.count);
      },
      error: function() {
        console.log('Failed!');
      },
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 2023-03-21
      • 1970-01-01
      • 2015-12-24
      • 2015-01-26
      • 1970-01-01
      • 2015-03-20
      • 2019-04-09
      • 2018-12-10
      相关资源
      最近更新 更多