为什么我的 Spotify 应用程序中对外部 API 的 JSON 请求被取消?

发布于 2024-12-20 04:54:31 字数 1413 浏览 2 评论 0原文

我正在构建一个 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 检查器快照

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

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

I'm building a Spotify App that pulls JSON data from a REST API, using URLs like

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

In my Spotify app manifest I have set the required permissions for my API host:

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

I have also configured Cross-Origin Resource Sharing in the response headers of my API. Here is a real example:

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

In Spotify, in the chrome inspector network tab, I see that the request is being cancelled:

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

If I run my app in Chrome on my desktop (outside of Spotify) the request is not cancelled and all is good.

What am I doing wrong? Or maybe this is a bug (feature?) in Spotify preview release?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如果没有 2024-12-27 04:54:31

有几件事可能会导致这种情况,我将按照从最容易解决到最难解决的顺序排列它们。

  1. 检查您的manifest.json 中是否具有正确的RequiredPermissions
  2. 检查您的manifest.json 是否具有有效的json 结构,您可以在http://jsonlint.com/
  3. 确保您没有存储带有 BOM 字符(文件开头的不可见字节)的文件,这可能会导致清单解析失败
  4. 确保您正在查询的服务器接受您的 起源。 Spotify 中的所有应用程序都有一个像 sp://appname 这样的来源,大多数服务器默认只接受 http 和 https 协议,因此您可以设置 Acces-Control-Allow-Origin > 到 * 以确保请求不会被取消。

最后,我想指出,即使请求有时在检查器中显示为已取消,它仍然会有正确的响应,因此请务必仔细检查。

希望这有帮助!

编辑:有时,出于某种奇怪的原因,它也有助于在 RequiredPermissions 中设置您请求的 url,而无需 http://https://前面加上了

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.

是伱的 2024-12-27 04:54:31

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

Have you tried restarting Spotify to reload changes to RequiredPermissions? I used to have a similar problem that was resolved upon a restart.

秋凉 2024-12-27 04:54:31

另一件需要警惕的事情是:

如果您正在进行本地开发。不要不要尝试向“localhost”或“127.0.0.1”发出 ajax/getJSON 调用,使用您的 Eth/WiFI 接口 IP

Spotify Web 引擎要么阻止请求中的这些本地主机标识符,要么忽略环回接口,或者 Windows 再次干扰。

这对某些人来说可能是显而易见的,但我花了一段时间才弄清楚。

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.

演多会厌 2024-12-27 04:54:31

Spotify 在内部使用 Chromium,因此遵守同源政策。要解决这个问题,请使用 JSONP。我建议在您的网络主机上设置来源策略以仅允许 Spotify。

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.

人│生佛魔见 2024-12-27 04:54:31

引起我问题的是 Bom 角色。

为了解决这个问题,我使用了 Notepad++,Encode ->采用无 BOM 的 UTF-8 编码

The one causing my problem was the Bom character.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文