ruby-on-rails - rails 响应跨域请求在本地开发,spotify 应用程序开发

标签 ruby-on-rails ruby api cross-domain spotify

所以我忙着开发一个 spotify 应用程序,试图让它与我的本地 Rails 应用程序 API 对话。当我尝试时,除了 req.status 0 之外,我什么也得不到。

我认为它要么是 spotify manifest.json 文件的问题,不允许 port:3000 继续你在所需权限中设置的 url,但它也在他们的文档中说了以下内容。

https://developer.spotify.com/technologies/apps/tutorial/

If you need to talk to an outside web API you're welcome to, as long as you abide by the rules set in the Integration Guidelines. Please note that when talking with a web API, the requests will come from the origin sp://$APPNAME (so sp://tutorial for our example) - make sure the service you are talking to accepts requests from such an origin.

所以,我不确定 rails 是否设置为不允许这种事情,或者是否将端口置于所需的权限中存在问题,但我的请求

  var req = new XMLHttpRequest();
  req.open("GET", "http://127.0.0.1:3000/api/spotify/track/1.json", true);
  console.log(req);
  req.onreadystatechange = function() {

  console.log(req.status);
  console.log(req.readyState);
  if (req.readyState == 4) {
    if (req.status == 200) {
       console.log("Search complete!");
       console.log(req.responseText);
    }
    }
};

req.send();

总是返回状态 0,例如:

var req = new XMLHttpRequest();
req.open("GET", "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=" + city + "&api_key=YOUR_KEY_HERE", true);

req.onreadystatechange = function() {

    console.log(req.status);

    if (req.readyState == 4) {
      console.log(req);           
        if (req.status == 200) {
            console.log("Search complete!");
            console.log(req.responseText);
        }
    }
};

req.send();

至少会返回 403 响应。好像没有提出请求之类的?

有人知道会发生什么吗?

非常感谢!

最佳答案

当从 Spotify 应用程序与外部服务对话时,即使它们在您的本地机器上运行,您也需要确保两件事正确到位:

  • URL(或至少是主机)位于 list 的 RequiredPermissions 部分。端口无所谓。 http://127.0.0.1 应该适合您的情况。

  • 服务器允许请求的 sp://your-app-id 来源,如您在问题中粘贴的文档中所述。这是通过在服务的 HTTP 响应中设置 Access-Control-Allow-Origin header 来完成的。人们通常将其设置为 Access-Control-Allow-Origin: * 以允许任何东西向他们的服务发出请求。

关于ruby-on-rails - rails 响应跨域请求在本地开发,spotify 应用程序开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805353/

相关文章:

ruby-on-rails - 设备升级后无法登录

ruby-on-rails - 事件记录翻译出错

ruby-on-rails - 如何从哈希中获取数据?

mysql - 保存在 MySQL 数据库中的 JSON 或 XML 或序列化数组

api - Twitter API限制

api - postman 为 POST 请求解析 "Invalid CORS request"

javascript - 使用 Trello API 的 client.js 库

ruby-on-rails - 使用 'find_all_by_'时出错

ruby-on-rails - 在lib/tasks任务中调用任务时, cucumber 方案不运行

mysql - 导轨数据库 :migrate doesn't work after altering the database manually