简体   繁体   中英

How to receive Yaml content in an AJAX call?

In my application, there is an endpoint that sends me the raw contents of a Yaml file in response to an AJAX call. I want to display them as they are in UI. The console throws an obvious error, which is for invalid JSON. How would I do it?

Update:

This is the snippet used for reading the file and sending the response.

filename = __file__ # Select your file here.                                
wrapper = FileWrapper(file(filename))
response = HttpResponse(wrapper, content_type='text/plain')
response['Content-Length'] = os.path.getsize(filename)
return response

Is there a way I could form a dictionary there with the contents of the file and then send the response?

From server, use jsonify on the raw content, pack it and ship it over to client.

repacked_json = json.dumps(raw_yaml_data) 
json_obj = json.loads(repacked_json)
return jsonify(result = json_obj)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM