我正在尝试通过ajax和wtforms验证表单,我有一个包含这样一个字段的表单:
const urlP = require('url');
const path = require('path');
const http = require('http');
const connect = require('connect');
const har = require('./har');
const proxiedServer = 'http://localhost:8888';
// Proxy server
http.createServer((req, res) => {
console.log(`Proxy: Got ${req.url}`);
const _r = http.request(
Object.assign(
{},
urlP.parse(proxiedServer),
{
method: req.method,
path: req.url
}
),
_res => {
res.writeHead(_res.statusCode, _res.headers);
_res.pipe(res);
}
);
req.pipe(_r);
}).listen(3124, () => {
console.log("Listening on 3124");
});
const middleware = connect();
middleware.use(har({
harOutputDir: path.resolve(process.cwd(), 'har/')
}));
middleware.use((req, res) => {
console.log('Proxied server: ', req.url);
let b = '';
req.on('data', c => {
b += c;
});
req.on('end', () => {
console.log('Proxied server: ', b);
});
res.writeHead(200);
res.end('ok');
});
http.createServer(middleware).listen(8888, () => {
console.log('proxied server listening on 8888');
});
我的服务器端代码是这样的:
name = StringField('Name :', description = "enter your name", validators=[InputRequired(message='please enter your name')])
我通过json发送字段数据然后我将其设置为字段,我检查它并且它的数据存在于myform.data中但是在验证后仍然存在InputRequired错误。
答案 0 :(得分:0)
您可以在表单类本身内验证表单。
views.py
这样您只需从sender.py
文件中的视图函数调用validate方法进行验证。
答案 1 :(得分:0)
您通过ajax发送数据并希望服务器端验证只需使用request.form
@app.route('/validation', methods=['get', 'post'])
def validation():
data = request.get_json(force = True)
myform = MyForm(request.form)
if myform.validate():
#do something
#return somthing
else:
return jsonify(myform.errors)