views:

38

answers:

2

I'm using the Ajax Form jQuery plugin to get JSON from a server:

/**
 * Helper function for the jQuery AJAX form plugin.
 */
function bindOnSuccess(form, callback) {
    form.ajaxForm({
        dataType: 'json',
        success: function (response, status, xhr, $form) {
            callback(response);
        }
    });
}

Usage:

bindOnSuccess($('#course-search'), function(response) {
    if (!response) {
        $("#system-status").text("Sorry, no course could be found for that search.");
    }
    else {
        $(".dept-code").text(response['course']['_dept_code']);
        $(".course-number").text(response['course']['_number']);
        $(".course-title").text(response['course']['_title']);

        $("#div-unparsed-pre-reqs").show();
        $("#unparsed-pre-reqs-teaser").show();
        $("#unparsed-pre-reqs").text(response['course']['_unparsed_pre_reqs']).hide();

        $("#td-required-for").text(response['analysis']['cRequiredFor']);

        loadNewJson(response['graph']);
    }
});

The JSON is correctly eval'd. However, part of it surprises me:

{"course": { 'foo': 'bar', /* data */},
 "analysis": { 'baz': 'odp', /* data */},
 "graph": "[{\"adjacencies\": [], \"id\": 2539, \"name\": \"BEE 3310: Bio-Fluid Mechanics\"}, {\"adjacencies\": [{\"nodeTo\": 2539, \"data\": {\"$direction\": [3332, 2539]}}], \"id\": 3332, \"name\": \"UNKNOWN 9999: UNKNOWN\"}]"}

Why is it that response['course'] and response['analysis'] get parsed, but response['graph'] remains a string?

UPDATE: I just tried it again, and it worked. I'm not sure that I changed anything. Odd.

The server code (Python) looks something like this:

result = {'course': dict_course, 'analysis': analysis, 'graph': jit_graph_data}
self.response.out.write(json.dumps(result))
A: 

The only way you could get that output is if jit_graph_data was already JSON, either because it was encoded somewhere earlier in the pipe, or because it was never decoded when pulled from its source.

Ignacio Vazquez-Abrams
A: 

To add to Ignacio's answer, you could fix your server-side code to use this:

result = {'course': dict_course, 'analysis': analysis, 'graph': json.loads(jit_graph_data)}
self.response.out.write(json.dumps(result))
icktoofay
Oh, I didn't read the full question, and you fixed it already? Oh well, I'll leave this here anyways...
icktoofay