Iterating through JSON within a $.ajax success

后端 未结 2 1853
谎友^
谎友^ 2021-01-17 17:38

When a user clicks a button I want to return some data and iterate through the JSON so that I can append the results to a table row.

At this point I am just trying t

2条回答
  •  一整个雨季
    2021-01-17 18:19

    You need to add:

    dataType: 'json',
    

    .. so you should have:

    $("#button").click(function(){
    
    $.ajax({
        url: 'http://localhost/test.php',
        type: 'get',
        dataType: 'json',
        success: function(data) {  
         $.each(data.COLUMNS, function(item) {
            console.log(item);
            });
        },
        error: function(e) {
            console.log(e.message);
        }
    });
    
    });
    

    .. as well as ensuring that you are referencing COLUMNS in the each statement.

    getJson is also another way of doing it..

    http://api.jquery.com/jQuery.getJSON/

提交回复
热议问题