简体   繁体   English

rails响应跨域请求在本地开发,发现应用程序开发

[英]rails responding to cross domain request developing locally, spotify app development

So Im messing around with developing a spotify app, trying to get it to talk to my local rails application API. 因此,我正在开发一个spotify应用程序,试图让它与我的本地rails应用程序API交谈。 I cant get anything other than a req.status 0 when I try it. 当我尝试它时,我无法得到除req.status 0之外的任何东西。

I think its either a problem with the spotify manifest.json file, not allowing the port:3000 to go on the url you set in required permissions, but it also says the following in their documentation. 我认为它是spotify manifest.json文件的一个问题,不允许port:3000进入你在所需权限中设置的url,但它也在他们的文档中说明了以下内容。

https://developer.spotify.com/technologies/apps/tutorial/ 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. 如果您需要与外部Web API通信,只要您遵守集成指南中规定的规则,即可。 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. 请注意,在与Web API交谈时,请求将来自原始sp:// $ APPNAME(因此sp://示例教程) - 确保您正在与之交谈的服务接受来自此类来源的请求。

So, Im not sure if rails is set to not allow this sort of thing, or if its an issue with the putting the port into the required permissions, but my request 所以,我不确定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();

Always returns status 0 where as their example: 始终返回状态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();

Will return a 403 response at least. 至少会返回403响应。 its like the request is not being made or something? 它就像请求没有被制作或什么?

Anyone have any idea what might be going on? 任何人都知道可能会发生什么?

Much appreciated! 非常感激!

When talking to external services from a Spotify App, even if they're running on your local machine, you need to make sure that two things are in place correctly: 当与Spotify应用程序中的外部服务交谈时,即使它们在本地计算机上运行,​​您也需要确保正确地准备好两件事:

  • The URL (or at least the host) is in the RequiredPermissions section of your manifest. URL(或至少是主机)位于清单的RequiredPermissions部分中。 Port doesn't matter. 港口没关系。 http://127.0.0.1 should be fine for your case. http://127.0.0.1应该适用于您的情况。

  • The server is allowing the sp://your-app-id origin for requests, as noted in the documentation you pasted in your question. 服务器允许sp://your-app-id来源请求,如您在问题中粘贴的文档中所述。 This is done by setting the Access-Control-Allow-Origin header in your service's HTTP response. 这是通过在服务的HTTP响应中设置Access-Control-Allow-Origin标头来完成的。 People often set it to Access-Control-Allow-Origin: * to allow anything to make requests to their service. 人们经常将其设置为Access-Control-Allow-Origin: *以允许任何内容向其服务发出请求。

Thanks for help, I got it figured out, I think it was multiple things, with one main Im an idiot moment for not trying that earlier 感谢您的帮助,我弄明白了,我认为这是多方面的事情,其中​​一个主要因素是一个白痴时刻

First off, I had to run rails on port 80, as obviously if Im accessing my site from 127.0.0.1:3000, thats not going to work if spotify app is requesting 127.0.0.1 unless I can load that directly in the browser, which you cannot unless you run on 80. That is done via 首先,我必须在端口80上运行rails,显然如果我从127.0.0.1:3000访问我的网站,如果spotify应用程序请求127.0.0.1则不会起作用,除非我可以直接在浏览器中加载它,你不能,除非你跑80.这是通过

rvmsudo rails server -p 80

Need to use rvmsudo because changing port requires permissions. 需要使用rvmsudo,因为更改端口需要权限。

Next I had to set access controll allow origin as noted above, that can be done in rails 3 by adding before filter to your app controller as follows. 接下来我必须设置访问控制允许来源,如上所述,可以通过在过滤器之前添加到您的应用控制器,如下所示在rails 3中完成。

class ApplicationController < ActionController::Base

  logger.info "I SEE REQUEST"

  before_filter :cor

  def cor
    headers["Access-Control-Allow-Origin"] = "*"
    headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",")
    headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",")
    head(:ok) if request.request_method == "OPTIONS"
  end
end

Finally, and most importantly (sigh), you cant just righclick and reload your spotify app when you make changes to your manifest file, exit spotify completely and restart it! 最后,也是最重要的(叹气),当你对清单文件进行更改时,你不能再点击并重新加载你的spotify应用程序,完全退出spotify并重新启动它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM