admin管理员组

文章数量:1420966

I am having problems with a JSON AJAX callback when the returned JSON object contains no data. My code is as follows:

$.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

The error es back as:

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

I have tried various methods to check if there is data within the callback but none seem to work. I am very new to using JSON and is wondering whether I should add a different call back via the php page if there is no data to return, however would like to find out if there is a way to do this via the javascript.

I am having problems with a JSON AJAX callback when the returned JSON object contains no data. My code is as follows:

$.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

The error es back as:

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

I have tried various methods to check if there is data within the callback but none seem to work. I am very new to using JSON and is wondering whether I should add a different call back via the php page if there is no data to return, however would like to find out if there is a way to do this via the javascript.

Share Improve this question edited Mar 15, 2013 at 9:22 freakish 56.6k12 gold badges139 silver badges177 bronze badges asked Mar 15, 2013 at 9:00 SideshowSideshow 1,3616 gold badges28 silver badges50 bronze badges 1
  • I have tried various methods to check if there is data Which ones? – Maen Commented Mar 15, 2013 at 9:05
Add a ment  | 

3 Answers 3

Reset to default 2

$.ajax with post will return HTML in string format you need something like this!

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
    }
}

Here you can specify dataType to use as json

  $.ajax({
    type: 'POST',
    url: ajaxURL,
     data:data,
    dataType: 'json',
    success: function(data){
        JSON.parse(data);
    }

});

And in server side script you must have to encode data using json_encode function.

While fetching json via ajax, here are a few things to note (incase it catches your issue too)

1) Content-Type Json parsing will work fluently when Content-type: application/json A html fetch (meaning Content-Type: text/html or equivalent) needs manually parsing json as String.

2) Jquery version This shouldn't be a problem since it has subsided since version: 1.5 (you might be using latest one, 1.9) Here is a link to the json related bug: http://bugs.jquery./ticket/8108

For json intensive coding, people often use jquery-json (http://code.google./p/jquery-json/) which a wrapper over simple jquery. You may want to consider if fix isn't easy.

I hope it answers atleast partially. Thanks..

本文标签: javascriptjquery parseJSON() returning error when JSON is emptyStack Overflow