【发布时间】:2018-01-25 17:37:21
【问题描述】:
我是该领域的新手,感谢您的帮助
我有以下使用 JSON 的 JavaScript 代码(全部在 HTML 模板中)
我想要实现的是使用 JSON 格式将“模式”(正则表达式)写入本地保存在我的机器上的文本文件。当用户点击带有 id="commitButton" 的按钮时,该操作应该发生
请指教 :) ...我使用的是 Python 2.7
这不起作用 错误:我在网络浏览器 (G Chrome) 中遇到错误:
VM901:1 POST http://127.0.0.1:5000/main 500 (INTERNAL SERVER ERROR)
(anonymous) @ VM901:1
send @ jquery.min.js:4
ajax @ jquery.min.js:4
r.(anonymous function) @ jquery.min.js:4
(anonymous) @ main:118
dispatch @ jquery.min.js:3
q.handle @ jquery.min.js:3
以下是使用 JSON 的 JavaScript 代码(全部在 HTML 模板中)
<script>
var allPatt = [];
{% for line in f %}
allPatt.push({pattern:"{{line}}", currentStatus:"Current", prevStatus: "Current"})
{%endfor%}
$(function(){
$('#commitButton').click(function(){
$.post('/main', { commit_patterns : JSON.stringify(allPatt) })
.done(function(data){
console.info(data);
})
});
});
</script>
在 Python 中我有以下代码:
from flask import Flask, request, render_template, jsonify
from pollerag.config import Config
import os, json
lines = []
app = Flask(__name__)
@app.route('/main', methods=["POST"])
def commitPattern():
data = json.loads(request.form['commit_patterns'])
for p in data:
if (p.currentStatus == "New" or p.currentStatus == "Current"):
with open('C:\\poller-admin-gui\\filter_configs\\field_pattern_filter.txt', 'w') as outfile:
json.dump(p, outfile)
if __name__ == '__main__':
app.run(debug = True)
【问题讨论】:
-
现有代码中有什么/不起作用?
-
它不写入文件:
-
我在浏览器中收到此错误:VM558:1 POST 127.0.0.1:5000/main500 (INTERNAL SERVER ERROR) (anonymous) @ VM558:1 send @ jquery.min.js:4 ajax @ jquery。 min.js:4 r.(匿名函数)@jquery.min.js:4(匿名)@main:117 dispatch@jquery.min.js:3 q.handle@jquery.min.js:3
-
编辑问题并添加错误输出以及意外的“不写入文件”。
-
在您的 Python 代码中,您不断覆盖同一个文件。我确定这不是您想要的。
标签: javascript python json python-2.7 flask