我有这个正在进行AJAX调用的脚本(JSONP)
$("#searchbox").autocomplete({
source: function(request, response) {
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
dataType: 'JSONP',
data: {
format: 'json',
q: 'select * from xml where url="http://google.com/complete/search?hl=nl&output=toolbar&q=' + encodeURIComponent(request.term) + '"'
},
success: function(data) {
if (typeof data == 'string') data = $.parseJSON(data);
response($.map(data.query.results.toplevel.CompleteSuggestion, function(item) {
return { label: item.suggestion.data, value: item.suggestion.data };
}));
}
});
},
select: function(e, ui){
},
open: function(){
doSearch($('.ui-autocomplete li:first-child a').text(), true, false);
$(".ui-autocomplete :first-child a").addClass("ui-state-hover");
$("#searchbox").focus();
return false;
},
select: function(e, ui){
$("#searchbox").autocomplete('search', ui.item.value);
},
close : function (event, ui) {
val = $("#searchbox").val();
$("#searchbox").autocomplete( "search", val );
}
});
我已经谷歌搜索它,结果证明它是一个安全问题。因为reqeust(输出)来自其他域(即Bing)。我尝试过很多东西,但没有运气。 这是我的doctype等等:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="desktop.css" media="screen" rel="stylesheet" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Asap' rel='stylesheet' type='text/css'>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<script src="livesearch.js" type="text/javascript"></script>
<link href='http://fonts.googleapis.com/css?family=Loved+by+the+King' rel='stylesheet' type='text/css'>
</head>
从她开始,除了纯HTML之外别无其他。
答案 0 :(得分:1)
除非此服务支持jsonp
,否则您应该这样做:
jsonp
- 陈述答案 1 :(得分:0)
由于Web浏览器使用same origin policy,不允许XMLHttpRequests发出跨域请求。请改用JSONP
。