简体   繁体   English

javascript:MIME类型('text / html')无法执行,并且启用了严格的MIME类型检查

[英]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: 这是我建议的解决方案:

  1. 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. 这样,用户将在其后退按钮历史记录中没有立即重定向的页面。

  2. 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('='); }); 
  3. 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.

相关问题 拒绝执行脚本,因为 MIME 类型('text/html')不可执行,并且启用了严格的 MIME 类型检查 - Refused to execute script because MIME type ('text/html') is not executable, and strict MIME type checking is enabled Dropbox:MIME 类型 (&#39;text/html&#39;) 不可执行,并且启用了严格的 MIME 类型 &gt; 检查 - Dropbox: MIME type ('text/html') is not executable, and strict MIME type > checking is enabled MIME 类型 ('application/json') 不可执行,并且启用了严格的 MIME 类型检查 - MIME type ('application/json') is not executable, and strict MIME type checking is enabled 拒绝从 &#39;URL&#39; 执行脚本,因为它的 MIME 类型 (&#39;text/html&#39;) 不可执行,并且启用了严格的 MIME 类型检查 - Refused to execute script from 'URL' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled 拒绝从“ file_name.php”执行脚本,因为其MIME类型(“ text / html”)不可执行,并且启用了严格的MIME类型检查 - Refused to execute script from 'file_name.php' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled 启用了严格的 mime 类型检查,拒绝执行来自 ' 的脚本<url> ' 它的 mime 类型 ('text/plain') 是不可执行的,并且</url> - strict mime type checking is enabled, refused to execute script from '<url>' its mime type ('text/plain') is not executable, and 拒绝应用“ <URL> &#39;,因为它的MIME类型(&#39;text / html&#39;)不是受支持的样式表MIME类型,并且启用了严格的MIME检查 - Refused to apply style from '<URL>' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled 错误:MIME类型(&#39;application / json&#39;)无法执行,并且使用YQL语句启用了严格的MIME类型检查? - Error: MIME type ('application/json') is not executable, and strict MIME type checking is enabled with YQL statements? Angular 6:拒绝从“URL”执行脚本,因为它的 MIME 类型()不可执行,并且启用了严格的 MIME 类型检查 - Angular 6 : Refused to execute script from 'URL' because its MIME type( ) is not executable, & strict MIME type checking is enabled 拒绝执行脚本,因为它的 MIME 类型 ('application/gzip') 不可执行,并且启用了严格的 MIME 类型检查 - Refused to execute script because its MIME type ('application/gzip') is not executable, and strict MIME type checking is enabled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM