Django: Reading Array of JSON objects from QueryDict

前端 未结 2 668
误落风尘
误落风尘 2021-02-05 16:58

How do I pass a composite JSON structure via AJAX call from JS and on the server side, read it as a \"very similar\" data structure in python?

I understand that json for

2条回答
  •  情话喂你
    2021-02-05 17:52

    This is actually jQuery, not Django, being strange. Your test variable does not contain JSON, but actual JS objects. jQuery, for reasons best known to itself, parses this into some very weird format before posting, hence the result you get. If you did this instead (note the quotes around the whole thing):

    var test = '[{"id": 1},{"id": 2},{"id": 3}]';
    

    you'd find you get very nearly the QueryDict you expect: the only thing you then need to do is to call json.loads(ret['json_data']).

    Also for reasons that I can't understand. jQuery doesn't contain any functionality to convert your array of objects to JSON. You'll need to find a plugin or separate library for that.

提交回复
热议问题