我正在尝试在返回 JSON 数组(在 mimetype 应用程序/json 中)的服务上使用 JQuery 自动完成。我的开发基于以下示例: http: //jqueryui.com/autocomplete/#remote-jsonp,它从 Geonames 中检索 JSON。geonames 示例中正确的 json 类似于
{"totalResultsCount":8387672,"geonames":[{"countryName":"Iran","adminCode1":"23","fclName":"mountain,hill,rock,... ","countryCode":"IR","lng":49.133333,"fcodeName":"mountain","toponymName":"Kūh-e Zardar","fcl":"T","name":"Kūh-e Zardar","fcode":"MT","geonameId":1,"lat":32.983333,"adminName1":"Lorestān","population":0}]}
不幸的是,我的服务只为我提供以下服务:
["berlin; berlin-steglitz","berliner festspiele"]
我也在尝试解析数组,但即使我得到正确的 Http 200 并且我看到响应是正确的,我也无法解析数组或使用它。没有调用来自 JQuery 的 .ajex 函数“成功”(我想是因为它需要一个 json 内容并检索一个文本),并且“完整”返回一个数据对象,没有检索 responseText 或数据内容的方法. 我不能使用请求类型“文本”,因为该服务在另一个域中,并且我打破了跨域模式。我的代码如下。
$(function() {
$("#searchinput").autocomplete(
{
source : function(request, response) {
$.ajax(
{
url : "http://Mybackendservice.com/",
dataType : "jsonp",
data : {
query : request.term
},
complete : function(data) {
console.log(data);
for(i=0;i<data.length;i++){
console.log(data[i].parametername); /// do whatever you want here.
};
response($.map(data, function(n,i) {
return {
label : n,
value : i
}
}));
}
});
},
minLength : 2,
select : function(event, ui) {
console.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");
}
});
});
有人对如何处理解析这样一个数组的问题有一些建议吗?