gpt4 book ai didi

jquery - 如何使用 Ajax 提交 Flask-WTF 表单

转载 作者:行者123 更新时间:2023-12-01 04:28:04 24 4
gpt4 key购买 nike

我希望能够采用此表单 - 通过 stripe checkout 获取 stripe ID(代码已实现并工作),然后通过 ajax 提交表单并将 stripe id 插入表单中的隐藏值中。

什么 jQuery 代码可以让我做到这一点?

class signupForm(Form):
forename = StringField('Forename', validators = [ Required()])
surname = StringField('Surname', validators = [Required()])
username = StringField('Username', validators = [Required(), Length(min = 4, max = 20)])
password1 = PasswordField('Password', validators = [Required(), Length(min = 6, max=50)])
password2 = PasswordField('Password', validators = [Required(), Length(min = 6, max=50)])
email = StringField('Email', validators = [Email(), Required()])
phone = StringField('Phone number', validators = [Required()])
addressLine1 = StringField('Address Line 1', validators = [Required()])
addressLine2 = StringField('Address Line 2')
town = StringField('Town', validators = [Required()])
county = StringField('County', validators = [Required()])
postcode = StringField('Postcode', validators = [Required()])
recurringDonationQuantity = StringField('If you would like to make a monthly recurring donation, please enter the amount in Sterling below. <br> Recurring Donation Amount')
stripetoken = HiddenField('')

我的页面:

            {% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert alert-warning" role="alert">
<ul>
{% for message in messages %}
<li>{{ message | safe }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}

{{ wtf.quick_form(form) }}

我的页面内也有这个 javascrpt

            <script>
var handler = StripeCheckout.configure({
key: '{{key}}',
image: '{{ url_for("static", filename="logo.png") }}',
locale: 'english',
token: function(token,args) {
$('#stripe-token').val = token;
wt
console.log(token)
}
});


$('#pay').on('click', function(e) {
// Open Checkout with further options

if ('{{type}}' == 'normal') {
description = 'Normal Membership';
amount = 2500;

console.log('membership type is NORMAL')
} else {
description = 'Associate Membership';
amount = 2500;

console.log('membership type is ASSOCIATE')
}



handler.open({
name: 'My Organisation',
description: description,
amount: amount,
currency: 'GBP',
panelLabel: 'Okay',
billingAddress: true,
zipCode: true
});

e.preventDefault();
});


// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>

最佳答案

我可能会使用 jQuery 的 serialize 尝试类似的事情方法。

<script>
$(document).ready(function(){
$('#submit').click(function() {
console.log('submit clicked');

//This part keeps you D.R.Y.
url_params = $(this).serialize();

//left this code the same...
var csrftoken = $('meta[name=csrf-token]').attr('content')

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)
}
}
})

$.ajax({
//more changes here
url: url_params, //you may need to modify this to hit the
//correct URL depending on your forms action param
//ex: url: $(this).attr('action') + url_params,
type: 'GET', //GET instead of POST so we can use url_params
contentType:'application/json;charset=UTF-8',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
</script>

希望这能为您指明正确的方向,以避免将如此多的字段硬编码到您的 JavaScript 中。

您还可以选择将 ajax 调用保留为 'POST' ,要做到这一点,您需要查看 jQuery 的 serializeArray 如果您想推出自己的解决方案,请参阅我改编自 this stackoverflow question 的以下代码:

// add a helper function to the $ jQuery object.
// this can be included in your page or hosted in a separate JS file.
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

// then inside your form page:
<script>
$(document).ready(function(){
$('#submit').click(function() {
console.log('submit clicked');

//This part keeps you D.R.Y.
data = $(this).serializeObject();

//left this code the same...
var csrftoken = $('meta[name=csrf-token]').attr('content')

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken)
}
}
})

$.ajax({
//more changes here
data: data,
url: '',
contentType:'application/json;charset=UTF-8',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
</script>

这里要讲的关键是您可以使用 javascript 或 jQuery 来读取 <form>数据并对其进行参数化。您希望避免对您的帖子进行硬编码 data如果可能的话。做起来很麻烦,改下来更麻烦。

关于jquery - 如何使用 Ajax 提交 Flask-WTF 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811069/

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