【发布时间】:2011-08-08 17:22:12
【问题描述】:
JS
$( "#ethnicbg" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: 'ethnic/',
type: 'POST',
dataType: "jsonp",
data: {
q: request.term
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item.title,
value: item.value
}
}));
}
});
},
minLength: 1,
select: function( event, ui ) {
// log( ui.item ?
// "Selected: " + ui.item.label :
// "Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
PHP
header('Content-type: application/json');
$url = $this->url->get();
$arr = get_data(); // returns [{"title":"White","value":"White"}]
echo json_encode($arr);
它返回数据,问题出在成功的地方:函数,
就在response($.map.... 之前,我输入了console.log('TEST') dnt 似乎根本没有去那里,或者提醒我做错了什么?
已解决
dataType: "jsonp", 应该是dataType: "json", 如果有人可以解释 json, jsonp 之间的区别吗?在这种情况下,它可以帮助我和其他可能遇到的人吗?
【问题讨论】:
-
JSONP 允许您向与请求页面不同的域中的服务器发出请求:en.wikipedia.org/wiki/JSONP
标签: ajax jquery-ui jquery autocomplete