How to return value from Python as JSON?

I am sending an ajax request from a jQuery file, as shown below, which is awaiting a response in JSON.

jQuery.ajax({ url: '/Control/getImageDetails?file_id='+currentId, type: 'GET', contentType: 'application/json', success: function (data){ alert(data); } }); }); 

In Python, I sent a response to an Ajax request as such:

  record = meta.Session.query(model.BannerImg).get(fid) return_info = [record.file_id, record.filename, record.links_to] return result_info 

This returns the parameters in plain text, which makes it impossible to read in different values. I believe sending a response from python since JSON solves this problem. Previously, I used a JSON server. How can I return the response as JSON?

+8
json jquery python ajax
source share
2 answers

return json.dumps(return_info)

Main problem -

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

because JSON format usually looks like

Example:

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

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

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

+12
source share

Encode it using functions in json .

+4
source share

All Articles