0
我有一个非常简单的HTML表单,只有一个<input type='text'>
字段,一个电子邮件地址,我需要通过AJAX传回Python脚本。我似乎无法接受另一端的价值。 (而且可以将所有的JSON编码/解码来避免,因为这只是一个场?)通过AJAX通过一个字段Flask
下面的代码:
from flask import Flask, render_template, request, json
import logging
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('index.htm')
@app.route('/start', methods=['POST'])
def start():
# next line outputs "[email protected]"
app.logger.debug(request.json);
email = request.json['email'];
# next line ALSO outputs "[email protected]"
app.logger.debug(email);
return json.dumps({ 'status': 'OK', 'email': email })
if __name__ == "__main__":
app.run()
和JavaScript从HTML side--
发送AJAX$("form").on("submit", function(event) {
event.preventDefault();
d = "email=" + $('#email').val(); // this works correctly
// next line outputs 'sending data: [email protected]'
console.log("sending data: "+d);
$.ajax({
type: "POST",
url: "{{ url_for('start') }}",
data: JSON.stringify(d),
dataType: 'JSON',
contentType: 'application/json;charset=UTF-8',
success: function(result) {
console.log("SUCCESS: ");
// next line outputs 'Object {email: "[email protected]", status: "OK"}'
console.log(result);
}
});
});