0
我想在返回JSON数组的服务上使用JQuery自动完成(在mimetype应用程序/ json中)。 我的开发基于此示例:http://jqueryui.com/autocomplete/#remote-jsonp,它从Geonames中检索JSON。在GEONAMES例如正确的JSON是一样的东西JSON数组的JQuery自动完成
{"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内容并检索文本),而“complete”返回的是一个没有方法的数据对象来检索responseText或者数据。 我无法使用requesttype“text”,因为该服务位于另一个域中,我打破了跨域模式。我的代码如下。
$(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");
}
});
});
任何人有如何处理解析这样的一个数组的问题的一些建议?
当你使用'sucess'而不是'complete'时会发生什么? (这里应该是'成功')。你是否也在提出跨域请求?否则,你不应该需要'jsonp'。 – 2013-03-25 17:09:05
@安德鲁·惠特克我提到它没有成功。并且我正在使用跨域请求,否则我只想去“文本” – 2013-03-25 17:10:56
“failure”方法是否得到执行?如果是这样,它是否会给你一个错误信息?另外,你确定你正在使用的服务器支持JSONP吗? – 2013-03-25 17:12:05