“资源被解释为脚本,但使用 MIME 类型应用程序/json 传输”使用 Youtube 的 JavaScript API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5232753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:16:33  来源:igfitidea点击:

"Resource interpreted as script but transferred with MIME type application/json" using Youtube's JavaScript API

javascriptjqueryyoutube-api

提问by DjangoRocks

I'm receiving a "Resource interpreted as script but transferred with MIME type application/json" error message using Google Chrome's JavaScript console.

我使用 Google Chrome 的 JavaScript 控制台收到“资源被解释为脚本但使用 MIME 类型应用程序/json 传输”错误消息。

I'm currently running the following code on my local computer:

我目前在本地计算机上运行以下代码:

var URL = "";
var YOUTUBE_ROOT = "http://gdata.youtube.com/feeds/api/videos?alt=jsonc&v=2";
var start_index = "&start-index=1";
var callback = "&jsonp=?"
function searchYouTube()
{
  var q = encodeURIComponent(jQuery("#query").val());
  var query = "&q="+q;
  URL = YOUTUBE_ROOT+start_index+query+callback; 
  alert(URL);
    $.getJSON(URL, function(data) {
        $.each(data.items, function(i, item) {
            alert(item);
        });
    });


}


jQuery(document).ready(function () {
     jQuery("#searchYouTube").click(searchYouTube);

});

May I know what is causing the error?

我可以知道是什么导致了错误?

I've tried using 'callback=?' , 'jsoncallback=?' for the callback, but all leads to the same error message.

我试过使用 'callback=?' , 'jsoncallback=?' 用于回调,但都导致相同的错误消息。

May I know how do i fix this?

我可以知道我该如何解决这个问题吗?

Best Regards.

此致。

回答by jujule

Since you use JSONP, you should code it like this IMHO :

由于您使用 JSONP,因此您应该像这样编码,恕我直言:

$.ajax(URL, {
    crossDomain:true, 
    dataType: "jsonp", 
    success:function(data,text,xhqr){
        $.each(data, function(i, item) {
            alert(item);
        });
    }
});

The correct parameter is callbackbut jQuery generates one automagically so dont specify it.

正确的参数是callbackjQuery 自动生成一个,所以不要指定它。

回答by Quentin

That's a warning, not an error, and shouldn't stop your code from working.

这是一个警告,而不是一个错误,并且不应阻止您的代码工作。

The fault is with YouTube for serving the data with the wrong content-type.

问题在于 YouTube 以错误的内容类型提供数据。

回答by John

This is a quirk in Chrome and how it differentiates an a XHR request from a typical browser request.

这是 Chrome 中的一个怪癖,以及它如何将 XHR 请求与典型的浏览器请求区分开来。

To prevent the message appearing and also allow chrome to render the response nicely as json in the console, append a query string to your request URL.

为了防止出现该消息并允许 chrome 在控制台中将响应很好地呈现为 json,请将查询字符串附加到您的请求 URL。

e.g

例如

var xhr_object = new XMLHttpRequest();

var url = 'mysite.com/party_in_my_pants'; // Using this one, Chrome throws error

var url = 'mysite.com/party_in_my_pants?'; // This one, Chrome is sexy.

xhr_object.open('POST', url, false);