我正在使用Craftyclicks邮政编码查询API,该API通过ajax返回结果并将其附加。
我也检查了所有库文件,但是没有问题。
它以JSON格式返回响应,但是不能附加到我的文本框中。
我检查了所有可能的方法,但仍无法正常工作
下面是我的代码
jQuery("#post_code").autocomplete({
source: function( request,response ) {
var enable_search = true;
if(enable_search){
jQuery.ajax({
url: "https://pcls1.craftyclicks.co.uk/json/rapidaddress",dataType: "jsonp",data: {
postcode: jQuery("#post_code").val(),response: 'data_formatted',lines: '2',sort: 'asc',key: "asdasd-asdasd-asdasd-asdasd"
},success: function(data) {
if(data.error_code != undefined){
//handle error message
alert(data.error_msg);
}
else{
var clear_data = new Array();
jQuery.each( data.delivery_points,function( index,item ) {
var fullLabel = item.department_name;
if(fullLabel != '' && item.organisation_name != '')
fullLabel = fullLabel + ',' + item.organisation_name;
else
fullLabel = fullLabel + item.organisation_name;
if(fullLabel != '' && item.line_1 != '')
fullLabel = fullLabel + ',' + item.line_1;
else
fullLabel = fullLabel + item.line_1;
if(fullLabel != '' && item.line_2 != '')
fullLabel = fullLabel + ',' + item.line_2;
else
fullLabel = fullLabel + item.line_2;
clear_data.push({
town: data.town,postcode: data.postcode,pcounty: data.postal_county,tcounty: data.traditional_county,dep_name: item.department_name,line_1: item.line_1,line_2: item.line_2,org: item.organisation_name,udprn: item.udprn,label: fullLabel,value: data.postcode
});
});
response( clear_data );
}
}
});
enable_search = 0;
}
},minLength: 0,select: function( event,ui ) {
jQuery("#town").val(ui.item.town);
if(ui.item.line_1!='' && ui.item.line_2 != ''){
jQuery("#address1").val(leading_caps(ui.item.line_1 +','+ ui.item.line_2));
var add1 = (leading_caps(ui.item.line_1 +','+ ui.item.line_2));
}else{
jQuery("#address1").val(leading_caps(ui.item.line_1 + ui.item.line_2));
var add1 = (leading_caps(ui.item.line_1 + ui.item.line_2));
}
jQuery("#post_code").val(ui.item.postcode);
jQuery("#companyname").val(ui.item.org);
//var div_data="<option>"+ui.item.org+","+add1+","+ui.item.town+"</option>";
//jQuery(div_data).appendTo('#ch_user1');
// do any action on selecting a result
},open: function() {
// do any action on showing the results
},close: function() {
jQuery("#post_code").autocomplete({ disabled: true });
jQuery("#post_code").blur();
// do any action on closing the results
}
});