javascript - jquery parseJSON() 在 JSON 为空时返回错误

标签 javascript jquery json

当返回的 JSON 对象不包含任何数据时,我在使用 JSON AJAX 回调时遇到问题。我的代码如下:

$.ajax({
    type: "POST",
    url: "includes/get_menu_name.php",
    headers: {"cache-control": "no-cache"},
    data: data,
    success: function(html) {
        //alert(html);
        var app_data = "";
        if (html.MenuData != 0) {
            $.each( $.parseJSON(html).MenuData, function() {
                app_data += "<li data-short='"+this['dish_short']+"' data-desc='"+this['dish_desc']+"' data-dish_id='"+this['dish_id']+"'>"+this['dish_name']+"</li>";
            });
            $('.listbox').show();
            $('.nameslist').html(app_data);
            $('li').hover(function() {
                $(this).addClass('hover2');
            },function(){
                $(this).removeClass('hover2');
            });
            if (html == "") {
                $('.listbox').hide();
            }

            $('li').click(function() {
                //alert($('li', this).data('short'));
                $('.price').val("");
                var main_name = $(this, 'li').text();
                $('.main_name').val(main_name);
                //$('.price').val($(this).find('.ajaxid').text());
                if(main_name.length > 40) {
                    $('.short_name').val($(this).data('short'))
                } else {
                    $('.short_name').val(main_name);
                }
                if($(this).data('desc')!="") {
                    $('.dish_desc').val($(this).data('desc'));
                }
                var dish_id=$(this).data('dish_id');
                $('.main_name').data('dish_id', dish_id);
                $('.listbox').hide();
            });
        }
    }
});//end ajax

错误返回为:

TypeError:$.parseJSON(...) is null

我尝试了各种方法来检查回调中是否有数据,但似乎都没有用。我对使用 JSON 很陌生,想知道如果没有数据要返回,我是否应该通过 php 页面添加不同的回调,但是想知道是否有办法通过 javascript 执行此操作。

最佳答案

$.ajaxpost 将返回字符串格式的 HTML 你需要这样的东西!

success:function(html)
{
    if(html)
    {
        try
        {
            html = JSON.parse(html);
            if(html.MenuData)
            {
                // do something interesting
            }
            else
            {
                // failed
            }
        }
        catch(e)
        {
            // failed
        }
    }
    else
    {
        // failed because response is empty
    }
}

关于javascript - jquery parseJSON() 在 JSON 为空时返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15428266/

相关文章:

javascript - chartjs.org 长标签问题

jquery - 使用 jQuery UI datepicker 选择日期时触发函数

javascript - 如何在 localStorage javascript 中堆叠多个数组?

javascript - JSON 响应逐个字符发送?

JavaScript/jQuery : how do you dynamically turn attribute values into object keys?

javascript - React "lifting state up"哲学 : How to avoid ending up with one big parent component containing all the code?

javascript - 在 ng-repeat 之后执行 jQuery 函数

javascript - 手机号码的正则表达式使括号作为国家代码的可选,破折号、点和空格是可选的

android - 如何在 android 中以编程方式检查 Web 服务响应是否是有效的 json?

PHP/jQuery - 如何将多维 PHP 数组转换为 JSON 字符串?