How to return value from Python as JSON?

后端 未结 2 1603
梦如初夏
梦如初夏 2021-02-14 06:23

I sending ajax request from a jQuery file like below, which expects response in JSON.

jQuery.ajax({
    url: \'/Control/getImageDetails?file_id=\'+currentId,
            


        
相关标签:
2条回答
  • 2021-02-14 07:02

    Encode it using the functions in the json module.

    0 讨论(0)
  • 2021-02-14 07:12

    return json.dumps(return_info)

    main problem is

    return_info = [record.file_id, record.filename, record.links_to]
    

    because JSON format is generally like

    Example:

    json.dumps({'file_id': record.file_id, 'filename': record.filename , 'links_to' : record.links_to})
    

    and the message you are going to receive is [object Object] if you use alert(data)

    So use alert(data.file_id); if you use the example

    0 讨论(0)
提交回复
热议问题