我正在 Joomla 中开发一个价格计算器,并且我创建了一个用于我的 ajax 函数的插件。
看起来它工作正常,但是当我获取数据时,我无法输出它。
这是我的 ajax 请求的 jquery 代码:
jQuery(document).ready(function(){
jQuery("#postcode-search").keyup(function(){
jQuery('#priceList ul').html('');
jQuery('#priceList').hide();
jQuery.ajax({
type: "POST",
url: "index.php?option=com_ajax&plugin=PriceCalculator&format=json",
dataType: "json",
data:'keyword='+jQuery(this).val(),
beforeSend: function(){
jQuery("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
},
success: function(data){
console.log(data);
jQuery("#suggesstion-box").show();
jQuery("#suggesstion-box").html(data.Postcode);
jQuery("#search-box").css("background","#FFF");
}
});
});
});
我通过控制台日志得到这个数组:
"[{"id":"1110","Postcode":"W11 1AA","Heathrow":"43","Luton":"58","Stansted":"58","Gatwick":"53","pcat":"W11"},{"id":"1111","Postcode":"W11 1AB","Heathrow":"43","Luton":"58","Stansted":"58","Gatwick":"53","pcat":"W11"},{"id":"1112","Postcode":"W11 1AD","Heathrow":"43","Luton":"58","Stansted":"58","Gatwick":"53","pcat":"W11"},{"id":"1113","Postcode":"W11 1AE","Heathrow":"43","Luton":"58","Stansted":"58","Gatwick":"53","pcat":"W11"},{"id":"1114","Postcode":"W11 1AF","Heathrow":"43","Luton":"58","Stansted":"58","Gatwick":"53","pcat":"W11"}]"
请问我做错了什么?
验证您的 api 是否返回有效的 json,并确保在响应标头中设置了“Content-Type: application/json”。
https://www.getpostman.com/是开发RESTAPI当一个有价值的工具:■它可以帮助您调试问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句