简体   繁体   English

如何在AJAX呼叫中接收Yaml内容?

[英]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. 在我的应用程序中,有一个端点向我发送Yaml文件的原始内容,以响应AJAX调用。 I want to display them as they are in UI. 我想按用户界面显示它们。 The console throws an obvious error, which is for invalid JSON. 控制台会抛出一个明显的错误,这是无效的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. 在服务器上,对原始内容使用jsonify ,打包并将其发送到客户端。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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