gpt4 book ai didi

javascript - 为什么我会看到此 getJSON 错误

转载 作者:行者123 更新时间:2023-11-29 10:21:55 25 4
gpt4 key购买 nike

我正在尝试使用 rottentomatoes movie APItwitter's bootstrap typeahead plugin但我不断收到以下错误:

XMLHttpRequest cannot load http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=MY_API_KEY&page_limit=5&q=t&format=jsonp. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

我的代码是这样的:

    var autocomplete = $('#searchinput').typeahead()
.on('keyup', function(ev){

ev.stopPropagation();
ev.preventDefault();

//filter out up/down, tab, enter, and escape keys
if( $.inArray(ev.keyCode,[40,38,9,13,27]) === -1 ){

var self = $(this);

//set typeahead source to empty
self.data('typeahead').source = [];

//active used so we aren't triggering duplicate keyup events
if( !self.data('active') && self.val().length > 0){

self.data('active', true);

$.getJSON("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=API_KEY_REMOVED&page_limit=5",{
q: $(this).val()
}, function(data) {


//set this to true when your callback executes
self.data('active',true);

//Filter out your own parameters. Populate them into an array, since this is what typeahead's source requires
var arr = [],
i=data.movies.length;
while(i--){
arr[i] = data.movies[i].title
}

//set your results into the typehead's source
self.data('typeahead').source = arr;

//trigger keyup on the typeahead to make it search
self.trigger('keyup');

//All done, set to false to prepare for the next remote query.
self.data('active', false);

});

}
}
});

知道是什么导致了这个错误吗?

最佳答案

出于安全考虑,不允许跨浏览器调用,请参阅 CORS ,要么你必须创建一个代理,让你的代理调用跨域并返回结果,或者如果其他域的服务器支持你可以使用 jsonp

关于javascript - 为什么我会看到此 getJSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com