【发布时间】:2017-07-30 02:52:29
【问题描述】:
我正在尝试使用 node-spotify-api npm 包运行 spotify web api。为了需要这个包,我必须使用一个叫做browserify的东西,它允许我在浏览器中专门使用“require()”函数。我严格尝试在客户端运行 api。当我这样做时,这是我得到的错误 -
// I have my client ID and client secret ID delcared in variables
// Code below is suppose to execute the search function once the user enters a track
var query = $("#query").val().trim();
console.log(query)
spotify.search({ type: 'track', query: query }, function(err, data)
{
if (err) {
return console.log('Error occurred: ' + err);
}
console.log(data);
});
// Once executed, I get the error below
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access. If an opaque
response serves your needs, set the request's mode to 'no-cors' to
fetch the resource with CORS disabled.
bundle.js:120 Error occurred: RequestError: TypeError: Failed to fetch
我查看了错误,我正在查看的所有内容都将我重定向到在服务器端运行 api 的方向,如果可能的话,我不想这样做。关于如何严格运行这个客户端有什么想法吗?
【问题讨论】:
-
请向我们展示您尝试提出的实际 Spotify 请求。您指向的特定错误是跨域访问错误,这意味着您尝试访问的站点不允许跨域访问,或者您没有以正确的方式发出请求以执行跨域请求。我们需要更多地了解您尝试发出的 API 请求,以便了解它是哪一个。
-
我想知道这个Spotify API Web : OAuth without Node.js 是不是你要找的。span>
-
刚刚用 spotify 电话更新了我的问题。至于 OAuth 链接,我查看了同一个 repo,据我了解,该示例可从服务器端操作。如果可能,我想将其作为前端应用程序运行并将其部署为前端应用程序。本质上,我想知道您是否可以将 node.js 用于前端应用程序。
-
好吧,你不能将 node.js 用于在浏览器中运行的前端应用程序,因为 node.js 不在浏览器中运行。您必须编写基于浏览器的 Javascript 才能在浏览器中运行。例如,浏览器中没有
http模块。相反,您必须使用XMLHttpRequest或fetch()进行Ajax 调用。在浏览器中使用了 spotify 包装器。我想这可能就是你想要的。 -
我发现了一个叫browserify的东西——browserify.org允许我在浏览器中运行node.js;特别允许我使用 require(" ") 我仍然遇到了这个与 spotify 相关的错误。错误是 spotify 函数中的 console.log。我最初使用 ajax 调用来运行 spotify web api,它运行完美,这是一个前端应用程序。自从 spotify 更改了对 api 的访问权限后,我现在在将其作为前端应用程序运行时遇到了问题。
标签: node.js api npm browserify spotify