gpt4 book ai didi

javascript - 在 Python 中访问 Jquery 键/值

转载 作者:行者123 更新时间:2023-11-29 19:05:43 24 4
gpt4 key购买 nike

我有一个简单的表单,我需要访问表单的 jquery 代码的键/值和属性。此外,当我尝试使用 request.form['stripeToken'] 在我的 View 中创建客户时,它会在问题末尾提到错误。我需要使用键/值属性访问 Jquery 脚本中的以下字段:

$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);

代码如下:

J查询代码:

<form id="myForm" action="/yearly" method="POST">

<input type="hidden" id="stripeToken" name="stripeToken" />
<input type="hidden" id="stripeEmail" name="stripeEmail" />
<button id="customButton">Purchase</button>
</form>

<script type="text/javascript">
var handler = StripeCheckout.configure({
key: 'pk_test_YgHVTCLIMQLW4NV6ntnJPAXs',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function (token) {

$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#myForm").submit();
}
});

$('#customButton12').on('click', function (e) {
handler.open({
name: 'Yearly',
description: 'Yearly Charge',
amount: 9500
});
e.preventDefault();
});
$(window).on('popstate', function () {
handler.close();
});

</script>

View 如下:

@app.route('/yearly', methods=['GET', 'POST'])
def yearly_charge():


key = stripe_keys['publishable_key']
data = get_profile_data(session['auth_token'])
profile_data = data['Student']
student_id = profile_data.id
student = get_profile_data(session['auth_token'])['StudentProfile']
pkg = Package.query.filter_by(student_id=profile_data.id).first()
# customer
stripe_token = request.form['stripeToken']
email = request.form['stripeEmail']
if not pkg:

try:
customer = stripe.Customer.create(
email=email,
source=request.form['stripeToken']

)
print request.form
subscription = stripe.Subscription.create(
customer=customer.id,
plan="yearly",
)
student_id = profile_data.id
student.stripe_customer_id = customer.id
student.stripe_subscription_id = subscription.id
package = Package(
student_id=student_id,
stripe_id = customer.id,
student_email=request.form['stripeEmail'],
is_active=True,
package_type='yearly',
subscription_id=subscription.id
)
dbase.session.add(package)

dbase.session.commit()

except stripe.error.CardError as e:
# The card has been declined
body = e.json_body
err = body['error']
flash("You've successfylly subscribed for annual package.")

return redirect(url_for('new_template', key=key))

错误:

stripe.error.InvalidRequestError
InvalidRequestError: Request req_AMbjoYEtQR1d6Y: Invalid source object: must be a dictionary or a non-empty string. See API docs at https://stripe.com/docs'

最佳答案

您似乎正在尝试为 strip 实现自定义按钮。如果您不知道如何在 python 和 js 之间进行通信,那么有一种简单的方法可以避免编写所有代码。

<form action='/yoururl' method='post'>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ key }}"
data-description="Your Description"
data-amount="500"
data-locale="auto"></script>
<input type="submit" value='Subscribe' class='btn' style='height:33px;width:50%;color:white;'>
</form>
<script>
document.getElementsByClassName("stripe-button-el")[0].style.display = 'none';
</script>

代码中实际发生的是它隐藏了默认的条纹按钮。因此,任何输入按钮都将在标签内为您工作。您可以使用多个按钮,例如,如果您有另一个按钮,您可以使用不同的价格或其他变量,只需像下面这样更改脚本:

 <script>
document.getElementsByClassName("stripe-button-el")[1].style.display = 'none';
</script>

如果有第三个按钮,你可以这样做:

        <script>
document.getElementsByClassName("stripe-button-el")[2].style.display = 'none';
</script>

关于javascript - 在 Python 中访问 Jquery 键/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43041003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com