我如何可以查询在JavaScript中的iTunes搜索API? [英] How can I query the iTunes search API in javascript?

查看:547
本文介绍了我如何可以查询在JavaScript中的iTunes搜索API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询在App Store上给定的应用程序的信息,但是我不断收到以下错误。

  XMLHtt prequest无法加载https://itunes.apple.com/lookup?id=<some-app-id> ;.
无访问控制允许来源标头的请求的资源present。
原产地的http://www.<有的-网站&GT; .co.uk,因此是不允许访问。响应有HTTP状态code 501。

在code我使用执行请求如下:

有谁知道在哪里我可以去错了吗?

  VAR配置= {
        标题:{
            访问控制允许来源':'*',
            访问控制允许的方法:GET,
            访问控制允许报头:内容类型,X-要求,使用',
        }
    };    $ http.get(https://itunes.apple.com/lookup?id=<some-app-id>配置).success(
        功能(数据){
            //我得到了一些数据回来了!
        }
    );


解决方案

编辑:这里是显示我的成功的方法大致得到它变成一个AngularJS应用一个plunker:

http://plnkr.co/edit/QhRjw8dzK6Ob4mCu6T6Z?p=$p$ PVIEW

添加这些头文件到您的服务器不会改变正在发生什么。交叉起源头需要由iTunes的API来添加。

这是不会发生的,所以你需要做什么,而不是在您的网页使用JSONP风格回调。还有就是iTunes的搜索API页面上的例子。

<一个href=\"http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html\" rel=\"nofollow\">http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html


  

请注意:当创建为您的网站的搜索领域和脚本,你
  应该使用动态脚本标记您的XMLHTTP脚本调用请求。
  例如:


 &LT;脚本SRC =HTTPS://.../search parameterkeyvalue&安培;回调={在网页中的JavaScript函数名}/&GT;

注意'回调'参数存在。那是在你的JavaScript全局定义将调用来自于'src'中请求的url响应页面上的功能。这个函数会将数据到您的网页或应用程序。你必须弄清楚如何。

这是一个耻辱,这个文档中使用的语言不清晰,因为你必须做某种JSONP风格变通办法的,因为他们没有自己的API启用CORS。

如果您需要动态添加脚本标签(读取数据一次是不够的),你可以试试这个教程:

<一个href=\"http://stackoverflow.com/questions/13121948/dynamically-add-script-tag-with-src-that-may-include-document-write\">Dynamically添加脚本标记与SRC,其中可能包括文件撰写

在一般的API可能是旨在用于后端(没有受到交叉起源的问题),而不是客户端的抓取。

I am trying to query the App Store for information on a given app, however I keep getting the following error.

XMLHttpRequest cannot load https://itunes.apple.com/lookup?id=<some-app-id>. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://www.<some-website>.co.uk' is therefore not allowed access. The response had HTTP status code 501.

The code I'm using to execute the request is as follows.

Does anyone know where I may be going wrong?

var config = {
        headers:  {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With',
        }
    };

    $http.get("https://itunes.apple.com/lookup?id=<some-app-id>", config).success(
        function(data) {
            // I got some data back!
        }
    );

解决方案

Edit: here's a plunker showing my successful approach roughly getting it into an AngularJS app:

http://plnkr.co/edit/QhRjw8dzK6Ob4mCu6T6Z?p=preview

Adding those headers to your server won't change what is happening. The cross origin headers need to be added by the iTunes API.

That is not going to happen, so what you need to do instead is to use JSONP style callbacks in your webpage. There is an example on the iTunes search API page.

http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html

Note: When creating search fields and scripts for your website, you should use dynamic script tags for your xmlhttp script call requests. For example:

<script src="https://.../search?parameterkeyvalue&callback="{name of JavaScript function in webpage}"/>

Note the 'callback' parameter there. That is a function defined globally in your javascript on the page that will get called with the response from the request to the url in 'src'. That function puts the data into your page, or application. You'll have to figure out how.

It's a shame that the language used in this documentation is not clearer, because you must do some kind of JSONP style workaround since they don't have CORS enabled on their API.

If you need to dynamically add a script tag (fetching data once is not enough) you can try this tutorial:

Dynamically add script tag with src that may include document.write

The API in general is probably intended for use by backends (not affected by cross origin issues), not for client side fetching.

这篇关于我如何可以查询在JavaScript中的iTunes搜索API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆