Jquery ui 为什么jquery自动完成不能在https(安全页面)上工作?
我试图让jquery自动完成在https(安全页面)页面上工作,但它没有显示任何下拉列表。 我搜索了这个问题,发现它的安全问题 有人能告诉我如何在https页面上打开这个自动完成下拉列表吗 以下是我的jquery自动完成代码:Jquery ui 为什么jquery自动完成不能在https(安全页面)上工作?,jquery-ui,ssl,autocomplete,https,Jquery Ui,Ssl,Autocomplete,Https,我试图让jquery自动完成在https(安全页面)页面上工作,但它没有显示任何下拉列表。 我搜索了这个问题,发现它的安全问题 有人能告诉我如何在https页面上打开这个自动完成下拉列表吗 以下是我的jquery自动完成代码: function zipAutoCompletet(prefix) { jQuery("#" + prefix + "_zip").autocomplete({ source: function (request, response) { jQue
function zipAutoCompletet(prefix) {
jQuery("#" + prefix + "_zip").autocomplete({
source: function (request, response) {
jQuery.ajax({
url: "http://ws.geonames.org/postalCodeSearchJSON",
dataType: "jsonp",
data: {
style: "full",
maxRows: 12,
postalcode_startsWith: request.term
},
success: function (data) {
response(jQuery.map(data.postalCodes, function (item) {
return {
label: item.placeName + (item.adminCode1 ? ", " + item.adminCode1 : "") + ", " + item.postalCode + ", " + item.countryCode,
value: item.postalCode
}
}));
jQuery('.ui-autocomplete').css('width', '188px');
}
});
},
minLength: 2,
select: function (event, ui) {
var myString = new String(ui.item.label);
var address = myString.split(',')
jQuery('#' + prefix + '_city').val(address[0]);
jQuery('#' + prefix + '_city').addClass('activated');
jQuery('#' + prefix + '_city').trigger('change');
jQuery('#' + prefix + '_city').parents('.row').removeClass('error-row')
jQuery('#' + prefix + '_city').parents('.row').addClass('ok-row')
var countryCode = address[3] ? address[3] : address[2]
countryCode = jQuery.trim(countryCode);
var countryName = jQuery('#' + prefix + '_country option[value="' + jQuery.trim(countryCode) + '"]').text()
jQuery('#countryContainer .jqTransformSelectWrapper span').html(countryName)
jQuery('#countryContainer .jqTransformSelectWrapper').addClass('selected-jqtranform');
jQuery('#' + prefix + '_country').parents('.row').addClass('ok-row')
jQuery('#' + prefix + '_country').parents('.row').removeClass('error-row')
jQuery('#' + prefix + '_country').val(jQuery.trim(countryCode))
var stateCode = address[2] ? address[1] : '';
stateCode = jQuery.trim(stateCode)
if (countryCode == 'US') {
var base = base_url;
base = base.replace("https", "http");
jQuery.ajax({
url: base + "/getStateName",
dataType: "jsonp",
data: {
stateCode: stateCode
},
success: function (data) {
stateName = data
jQuery('#jc_state').val(stateName);
jQuery('#jc_state').addClass('activated');
jQuery('#jc_state').parents('.row').removeClass('error-row')
jQuery('#jc_state').parents('.row').addClass('ok-row')
jQuery('#jc_state').trigger('change');
formValidate();
}
});
} else {
stateName = stateCode
jQuery('#jc_state').val(stateName);
jQuery('#jc_state').addClass('activated');
jQuery('#jc_state').parents('.row').removeClass('error-row')
jQuery('#jc_state').parents('.row').addClass('ok-row')
jQuery('#jc_state').trigger('change');
formValidate();
}
jQuery('#' + prefix + '_zip').parents('.row').addClass('ok-row')
jQuery('#' + prefix + '_zip').parents('.row').removeClass('error-row');
},
open: function () {
jQuery(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
jQuery(this).removeClass("ui-corner-top").addClass("ui-corner-all");
},
change: function (event, ui) {
if (ui.item === null) {
jQuery("#" + prefix + "_zip").parents('.row').removeClass('ok-row');
jQuery("#" + prefix + "_zip").parents('.row').addClass('error-row');
$("#" + prefix + "_zip").val('');
}
}
});
}
我在zipcode字段中使用上述代码。该代码在非https页面上运行良好,但在https页面上尝试时,它没有显示出来
欢迎使用任何解决方案。当我调查服务提供商时,他们支持的是
jsonp
,下面的示例成功了
$("input").autocomplete({
source: function (request, response) {
$.getJSON("http://ws.geonames.org/postalCodeSearchJSON?callback=?",
{ 'postalcode_startsWith': request.term, maxRows: 12, style: "full" },
function(data) {
if(data.postalCodes){
var x = $.map(data.postalCodes, function(v, i){
console.log(v)
return {
label: v.placeName + ' - ' + v.postalCode,
v: v.postalCode
}
});
response(x);
}
}
);
}
});
演示:您是否检查了同源策略条件同源策略的含义?您的页面的域名是什么?同源策略禁止浏览器向不同于页面域的域发送ajax请求。在您的情况下,发送ajax请求的页面必须来自
ws.geonames.org
域。在本页中,您可以查看原产地确定规则
,只需对@ArunPJohny所说的内容稍加补充,您的浏览器禁止跨不同域发送ajax请求,但您可能有兴趣使用它来实现这一点。JSONP(或带填充的json)允许跨不同域通信。谢谢@ArunPJohny,但是有什么方法可以完成这个任务吗?