为什么JSON请求我的外部API在我的Spotify应用被取消? [英] Why are JSON requests to my external API being cancelled in my Spotify App?

查看:164
本文介绍了为什么JSON请求我的外部API在我的Spotify应用被取消?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Spotify应用,从一个REST API拉JSON数据,使用URL像

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

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

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

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

我也曾在我的API的响应头配置跨来源资源共享。这是一个真实的例子:

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

在Spotify的,在Chrome网络督察选项卡中,我看到该请求被取消:

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的应用程序(外的Spotify)的请求,不取消,一切都很好。

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

我是什么做错了吗?或者,也许这是Spotify的preVIEW释放错误(功能?)?

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

推荐答案

一对夫妇的事情可能导致此,我会把他们为了从易到解析到最难的。

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


  1. 检查是否有正确的 RequiredPermissions 在你的manifest.json

  2. 检查您的manifest.json有一个有效的JSON strjcture,你可以像 http://jsonlint.com/ <做到这一点的网站/ LI>
  3. 确保你没有保存(在文件的起始字节看不见)以BOM字符的文件,这可能会导致明显的解析失败

  4. 确保您要查询服务器接受您的起源。在Spotify的所有的应用程序有一个像 SP原点://应用程序的名字,大多数服务器只接受默认的HTTP和HTTPS协议,这样你就可以设置艾策斯 - 控制允许来源 * ,以确保请求没有被取消。

  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.

希望这有助于!

编辑:有时候,出于某种奇怪的原因,这也有助于建立你的 RequiredPermissions 没有请求的URL 的http:// 的https:// prepended

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

这篇关于为什么JSON请求我的外部API在我的Spotify应用被取消?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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