Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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.

May I know how do i fix this?

Best Regards.

like image 881
DjangoRocks Avatar asked Mar 08 '11 12:03

DjangoRocks


1 Answers

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

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

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

like image 119
jujule Avatar answered Oct 14 '22 02:10

jujule