locked
How to use parseJSON in jquery ajax when server side function return json in string RRS feed

  • Question

  • User264732274 posted

    i got a sample but not sure does it work

    $.ajax({
        url: "data.json",
        //force to handle it as text
        dataType: "text",
        success: function(data) {
    
            //data downloaded so we call parseJSON function 
            //and pass downloaded data
            var json = $.parseJSON(data);
            //now json variable contains data in json format
            //let's display a few items
            for (var i=0;i<json.length;++i)
            {
                $('#results').append('<div class="name">'+json[i].name+'</>');
            }
        }
    });

    my asp.net server side function return json in string to client and i need to parse that json at client side. so above code will work?

    see this code too. the below code does not use parseJSON function. so the below code each can parse json when json return from server side in string format ?

    $.ajax({ 
        type: 'GET', 
        url: 'http://example/functions.php', 
        data: { get_param: 'value' }, 
        dataType: 'json',
        success: function (data) { 
            $.each(data, function(index, element) {
                $('body').append($('<div>', {
                    text: element.name
                }));
            });
        }
    });

    thanks

    Monday, February 1, 2016 9:15 AM

Answers

  • User475983607 posted

    my asp.net server side function return json in string to client and i need to parse that json at client side. so above code will work?

    Essentially, JQuery.parseJSON converts a JSON formatted string into an JSON object.  If the data response is a property formatted JSON string that represents a collection then yes it will work.

    see this code too. the below code does not use parseJSON function. so the below code each can parse json when json return from server side in string format ?

    The data type is defined a json so the serialization and deserialization is done for you.

    Are you receiving an error or unexpected results?  If so, please post all the relevant code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2016 12:48 PM