?Briella 2012-01-05 20:48 采纳率: 0%
浏览 390

403禁止JSONP请求

There is a domain I don't control which returns JSON data -- going to http://services.example.com/search?store=ITUNES&q=twitter yields something like

{
  MySearch: {
    resultCount: 1
    results: [
      { name: "twitter" }
    ]
  }
}

When I try to get it with jsonp from another domain, such as from my own computer (using jQuery) --

$.ajax({
    url: 'http://services.example.com/search?store=ITUNES&q=twitter',
    dataType: 'jsonp',
    success: function(data) {
        console.log('hi');
    }
});

I get "Status Code: 403 Forbidden".

Here are the server response headers:

Connection:keep-alive
Content-Length:9
Content-Type:text/plain
Date:Thu, 05 Jan 2012 20:33:04 GMT
Server:WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)
X-Frame-Options:sameorigin

Is there any way I can get this data without making any server-side changes?

  • 写回答

2条回答 默认 最新

  • weixin_33697898 2012-01-05 23:23
    关注

    This is due to the browser's cross origin resource sharing policy.

    To enable this you'd have to have the origin add special headers to "preflight" your request. Else, the JSONP tactic would allow you to access data across domains, but it has to actually be JSONP and not just JSON.

    Here's an example on how to enable cross-domain resource sharing in PHP:

    <?php
          header('Access-Control-Allow-Origin: http://sub.domain.com');
          header('Access-Control-Allow-Credentials: true' );
    ?>
    

    As you said, unfortunately I think this is out of your control. In fact, the security mechanisms are in place in part to prevent stuff like this from happening (i.e. sapping another servers resources by consuming it's JSON).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog