2

I'm quite new to JSON handling and I've got myself stuck. My page contains the following script;

<script type="text/javascript">



$(document).ready(function() {

    $('#progressBar').progressbar({value: 0.0});


    process();
});
    function process() {

        getStatus();
        setInterval(getStatus,1000);
    }

    function getStatus() {
        $.getJSON('status-report', function(data) {
            var statusBean = $.parseJSON(data);
              $('#progressBar').progressbar('option','value',$.trim(statusBean.percentComplete));
            $('#status').html(statusBean.statusDescription);
        });
    }


</script>

Using Firebug, I can see that the call to 'status-report' is returning a JSON string

{"statusBean":{"percentComplete":50.0,"statusDescription":"Default Description"}} 

but after $.parseJSON, Firebug shows me that the variable statusBean is null.

What am I doing wrong?

2
  • 3
    data is already a JavaScript object. getJSON parses the response for you. It is described in the documentation: "The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method.". Commented Dec 20, 2011 at 12:31
  • 1
    possible duplicate of why parseJSON returns null Commented Dec 20, 2011 at 12:35

1 Answer 1

3

The value of data as passed to the getJSON callback, will already be a JS object. $.parseJSON will return null when an object is passed to it, as it expects a string. The call to $.parseJSON can simply be removed here, since getJSON assumes JSON in the first place.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.