我无法从ajax调用输出json

时间:2019-01-20 13:14:28

标签: jquery json ajax joomla

我正在Joomla中使用价格计算器,并且创建了一个插件来用于我的Ajax函数。

似乎工作正常,但是当我获取数据时,我无法输出它。

这是我来自aj​​ax请求的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"}]"

enter image description here

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

验证您的api返回有效的json,并确保在响应标头中设置了“ Content-Type:application / json”。

https://www.getpostman.com/是开发RestAPI时的宝贵工具:它可能有助于您调试问题。