Python 无法从AJAX调用[Flask]接收数据
我试图做的是:从AJAX接收数据到Flask。最后,我想将来自条带的令牌数据发送到烧瓶端 问题是:我无法将任何数据打印到控制台。所以我假设数据没有被传递 我无法从Ajax调用接收数据。我已经搜索了一段时间,但还没有找到修复方法。我见过许多不同的解决方案,但都不适合我。我正在尝试实现一个自定义条带支付流 我最终计划做的是通过JSON格式的“data”参数传入令牌中我需要的所有数据。下面是代码的不同方面 index.html app.py 控制台上没有打印任何内容。我在这上面浪费了太多时间,所以任何线索或建议都将不胜感激 更新Python 无法从AJAX调用[Flask]接收数据,python,json,ajax,flask,stripe-payments,Python,Json,Ajax,Flask,Stripe Payments,我试图做的是:从AJAX接收数据到Flask。最后,我想将来自条带的令牌数据发送到烧瓶端 问题是:我无法将任何数据打印到控制台。所以我假设数据没有被传递 我无法从Ajax调用接收数据。我已经搜索了一段时间,但还没有找到修复方法。我见过许多不同的解决方案,但都不适合我。我正在尝试实现一个自定义条带支付流 我最终计划做的是通过JSON格式的“data”参数传入令牌中我需要的所有数据。下面是代码的不同方面 index.html app.py 控制台上没有打印任何内容。我在这上面浪费了太多时间,所以任何
我做了@Daniel Roseman建议的一些更新,但控制台上没有任何打印 您没有发布JSON 您不需要访问request.get_json,只需从字典中访问单个元素即可。在这种情况下:
token = request.form['token']
请尝试以下代码
在javascript中:
var data = {token: token}
$.ajax({
url: '/charge',
data: JSON.stringify(data),
type: 'POST',
success: function (response) {
console.log(response);
},
error: function (error) {
console.log("ERROR");
console.log(error);
},
dataType: "json",
contentType: 'application/json;charset=UTF-8',
});
在控制器[充电方法]中:
from flask import Flask
from flask import render_template, request, jsonify
@app.route('/charge', methods=['POST'])
def charge():
# Grab token information
token = request.json['token']
# This is the expected token from json
print(token)
# This test print statement below now prints to console
print("This print statement now runs surely")
# return JSON
return jsonify(
code=0,
msg="Success"
)
谢谢你的回答!我接受了你的建议,现在我不再在控制台中看到错误了!坏的一面是,现在没有任何东西打印到控制台,甚至连测试打印语句都没有。你知道为什么会这样吗?
var data = {token: token}
$.ajax({
url: '/charge',
data: JSON.stringify(data),
type: 'POST',
success: function (response) {
console.log(response);
},
error: function (error) {
console.log("ERROR");
console.log(error);
},
dataType: "json",
contentType: 'application/json;charset=UTF-8',
});
from flask import Flask
from flask import render_template, request, jsonify
@app.route('/charge', methods=['POST'])
def charge():
# Grab token information
token = request.json['token']
# This is the expected token from json
print(token)
# This test print statement below now prints to console
print("This print statement now runs surely")
# return JSON
return jsonify(
code=0,
msg="Success"
)