Python 处理条纹请求的烧瓶视图可以';我找不到stripeToken
我正在尝试将Stripe与Flask集成,但页面加载后会出现以下错误Python 处理条纹请求的烧瓶视图可以';我找不到stripeToken,python,flask,stripe-payments,Python,Flask,Stripe Payments,我正在尝试将Stripe与Flask集成,但页面加载后会出现以下错误 The browser (or proxy) sent a request that this server could not understand. 为什么会出现此错误?如何修复 @store.route('/payment',methods=['GET', 'POST']) def payment(): stripe.api_key = "test_key" token = request.form['
The browser (or proxy) sent a request that this server could not understand.
为什么会出现此错误?如何修复
@store.route('/payment',methods=['GET', 'POST'])
def payment():
stripe.api_key = "test_key"
token = request.form['stripeToken']
try:
charge = stripe.Charge.create(
amount=1000,
currency="cad",
source=token,
description="Example charge"
)
except stripe.error.CardError, e:
pass
return render_template("payment_form.html")
Stripe.setPublishableKey('test_key');
jQuery(函数($){
$(“#付款单”)。提交(功能(事件){
var$form=$(此);
$form.find('button').prop('disabled',true);
Stripe.card.createToken($form,stripeResponseHandler);
返回false;
});
});
函数stripeResponseHandler(状态、响应){
var$form=$(“#付款单”);
if(response.error){
$form.find('.payment errors').text(response.error.message);
$form.find('button').prop('disabled',false);
}否则{
var token=response.id;
$form.append($('').val(标记));
$form.get(0.submit();
}
};
您想要请求.form.get('stripeToken',None)
页面加载后立即失败的原因是加载页面是一个get
请求<代码>请求。表单在POST
请求中填写,该请求仅在您提交表单时发生request.form
是一个特殊的字典,它会引发400错误,而不是KeyError
仅在POST
请求期间处理表单数据。还记得在POST
之后重定向,这样浏览器就不会挂起过时的表单数据
from flask import request, redirect, flash, render_template
stripe.api_key = 'test_key'
@store.route('/payment', methods=['GET', 'POST'])
def payment():
if request.method == 'POST':
token = request.form['stripeToken']
try:
charge = stripe.Charge.create(amount=1000, currency="cad", source=token, description="Example charge")
return redirect(request.path)
except stripe.error.CardError, e:
flash('Error processing payment.', 'error')
return render_template('payment_form.html')
如果在开发过程中启用调试模式,您会发现调试应用程序错误要容易得多。:
app.run('localhost',debug=True)
。当应用程序出现错误时,这将在浏览器中为您提供一个交互式回溯调试器。我收到浏览器(或代理)发送了一个此服务器无法理解的请求错误现在您使用的是哪台服务器?您使用的是什么浏览器?服务器是什么意思?我正在使用Chrome,您是如何为您的应用程序提供服务的?您是否正在使用Flask开发服务器(通过app.run())或uWSGI或Apache或类似软件?Flask开发服务器