5

我正在构建一个 Spotify 应用程序,它使用像这样的 URL 从 REST API 中提取 JSON 数据

http://www.mydomain.com/api/command?option=value

在我的 Spotify 应用清单中,我为我的 API 主机设置了所需的权限:

"RequiredPermissions": [ "http://*.mydomain.com" ]

我还在我的 API 的响应标头中配置了跨域资源共享。这是一个真实的例子:

Server: nginx/0.7.65
Date: Thu, 08 Dec 2011 09:07:16 GMT
Content-Type: application/json
Connection: keep-alive
X-Powered-By: Mojolicious (Perl)
Set-Cookie: mojolicious=eyJwcmVmcyI6e30sImZpbHRlcnMiOnsicGllciI6eyJzb3VyY2VzIjpbMjBdfSwiZWxlYyI6eyJzb3VyY2VzIjpbMTMsMTddLCJ4dGFncyI6WyJzaG9lZ2F6ZSJdLCJ0YWdzIjpbImVsZWN0cm9uaWMiXX0sInB1bmtkdWIiOnsieHRhZ3MiOlsicmVnZ2FlIl0sInRhZ3MiOlsicHVuayIsImR1YiJdfX0sImV4cGlyZXMiOjEzMjMzMzg4MzZ9--c6d6214525b5d56785eebc99217394a1; Version=1; Path=/; expires=Thu, 08 Dec 2011 10:07:16 GMT
Content-Length: 23381
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: *

200 OK

在 Spotify 的 chrome 检查器网络选项卡中,我看到请求被取消:

Name: command www.mydomain.net/api 
Method: GET 
Status Text: (canceled) 
Type: undefined 
Initiator: jquery-1.js:7180 (Script) 
Size Content: 13B (0B) 
Time Latency: 21ms (0.0 days)

Chrome inspector snapshot

如果我在桌面上的 Chrome 中运行我的应用程序(在 Spotify 之外),请求不会被取消并且一切都很好。

我究竟做错了什么?或者这可能是 Spotify 预览版中的一个错误(功能?)?

4

5 回答 5

9

A couple of things can cause this, I'll put them in order from easiest to resolve to hardest.

  1. Check that you have the correct RequiredPermissions in your manifest.json
  2. Check that your manifest.json has a valid json strjcture, you can do this on websites like http://jsonlint.com/
  3. Make sure you're not storing the file with a BOM character (invisible byte at the start of the file), this can cause parsing of the manifest to fail
  4. Make sure that the server you are querying accepts your origin. All apps in Spotify have an origin like sp://appname, most servers only accept http and https protocols by default, so you can set the Acces-Control-Allow-Origin to * to make sure the request doesn't get cancelled.

Lastly, I'd like to note that even though a request sometimes shows up in the inspector as cancelled, it'll still have a correct response, so be sure to double-check that as well.

Hope this helps!

Edit: sometimes, for some weird reason, it also helps to set the url you're requesting in RequiredPermissions without the http:// or https:// prepended.

于 2011-12-14T11:10:16.277 回答
4

您是否尝试过重新启动 Spotify 以重新加载更改RequiredPermissions?我曾经遇到过类似的问题,重启后就解决了。

于 2011-12-12T19:30:11.017 回答
4

Another thing to be wary of:

If you are doing local development. Do NOT try to issue your ajax/getJSON calls to "localhost" or "127.0.0.1", use your Eth/WiFI interface IP instead.

The spotify web engine either blocks these localhost identifiers in requests, or the loopback interface is ignored, or windows has interfered again.

This may be apparent to some, but took me a while to figure it out.

于 2012-01-02T10:39:54.567 回答
0

Spotify uses Chromium inside and therefore adheres to the Same Origin policy. To get around it, use JSONP. I'd recommend setting the origin policy on your webhost to only allow Spotify.

于 2011-12-15T18:05:56.370 回答
0

The one causing my problem was the Bom character.

In order to fix that I used Notepad++, Encode -> Encode in UTF-8 without BOM

于 2012-01-23T16:51:53.297 回答