[英]javascript: MIME type ('text/html') is not executable, and strict MIME type checking is enabled
I am trying to make a simple get authorization request to the Spotify Api. 我正在尝试向Spotify Api进行简单的获取授权请求。 If i make a GET HTTPRequest then i get a cross-domain error.
如果我执行GET HTTPRequest,则会收到跨域错误。 Hence I making a JSONP request using a callback, but this leads to a MIME type error as shown above, whose workaround i have found is to again make an HTTPRequest JSON request to match the MIME type.
因此,我使用回调发出了JSONP请求,但这导致了MIME类型错误,如上所示,我发现其解决方法是再次发出HTTPRequest JSON请求以匹配MIME类型。 Quite a deadlock I am in here !
我陷入了僵局! Please help!
请帮忙! Thanks
谢谢
This is my js code block: 这是我的js代码块:
(function(){
var script = document.createElement('script');
script.src = 'https://accounts.spotify.com/authorize?client_id=CLIENT_ID&response_type=code&redirect_uri=https://samcasm.github.io/moodsetNow/moodset.html&scope=user-read-private%20user-read-email&state=34fFs29kd09?callback=mySpotify';
document.getElementsByTagName('body')[0].appendChild(script);
})();
function mySpotify(){
console.log(response);
}
Your problem seems to be that you're using a <script>
tag to load something that's an HTML page. 您的问题似乎是您正在使用
<script>
标记来加载HTML页面。 This is my suggested solution: 这是我建议的解决方案:
When the user needs to authenticate, redirect them: 当用户需要进行身份验证时,请重定向他们:
location.href = "https://accounts.spotify.com/authorize" + "?client_id=" + CLIENT_ID + "&response_type=token" + "&redirect_uri=" + encodeURIComponent(THE_URI_TO_REDIRECT_TO) + "&state=" + STATE + // optional "&scope=" + SCOPES.join(" ") + // optional "";
Note that if you're going to redirect on page load, use location.replace(...)
instead of location.href = ...
. 请注意,如果要在页面加载时重定向,请使用
location.replace(...)
而不是location.href = ...
This way, the user will not have the immediately-redirecting page in their back button history. 这样,用户将在其后退按钮历史记录中没有立即重定向的页面。
Then, at the URL in THE_URI_TO_REDIRECT_TO
, parse the hash: 然后,在
THE_URI_TO_REDIRECT_TO
中的URL THE_URI_TO_REDIRECT_TO
,解析哈希值:
Spotify makes a hash like this: #access_token=...&expiry=...
. Spotify进行这样的哈希处理:
#access_token=...&expiry=...
location.hash
returns that hash, including the leading #
. location.hash
返回该哈希,包括开头的#
。 First, we set up our object that will hold the options: 首先,我们设置将包含选项的对象:
var hash = {};
then, we remove the #
: 然后,我们删除
#
:
var h = location.hash.slice(1)
… and split on the &
s. …并在
&
上分割。
h = h.split('&')
Next, we iterate over all of the pairs ( forEach
) and put the two pieces in the hash
object (ie hash['access_token'] = '...';
接下来,我们遍历所有对(
forEach
)并将这两部分放hash
对象(即hash['access_token'] = '...';
h.forEach(function(pair) { pair = pair.split('='); hash[pair.shift()] = pair.join('='); });
After that, you can read the data. 之后,您可以读取数据。
if (hash.error) { console.log(hash.error); } else { var token = hash.access_token; hash.token_type === "Bearer"; var expiry = new Date(); expiry.setSeconds(expiry.getSeconds() + (+hash.expires_in)); } var state = hash.state; // optional
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.