我有一段JavaScript處理一些我的表單數據,結果需要提交給我的代碼隱藏,因此我可以將它映射到一個對象並保存到數據庫:如何將JavaScript方法(JSON對象)的結果發送到asp.net webform上的代碼隱藏?
以下是表單:
<form method="POST" id="paymentForm">
<span class="payment-errors" runat="server"></span>
<div>
<label>
<span>Card Number</span>
<br />
<input type="text" size="20" data-stripe="number" runat="server" placeholder="1111222233334444" />
</label>
</div>
<div>
<label>
<span>CVC</span>
<br />
<input type="text" size="4" data-stripe="cvc" runat="server" placeholder="123"/>
</label>
</div>
<div>
<label>
<span>Expiration (MM/YYYY)</span>
<br />
<input type="text" size="2" data-stripe="exp-month" runat="server" placeholder="01"/>
</label>
<br />
<input type="text" size="4" data-stripe="exp-year" runat="server" placeholder="2020"/>
</div>
<button type="submit">Submit Payment</button>
</form>
這對JS位:
<script type="text/javascript">
// This identifies your website in the createToken call below
// You need to put your real publish key here.
Stripe.setPublishableKey('pk_test_1nDJ3hA1Mv2Sy9bUoYcBMXmm');
// ...
// I am using jquery to process the payment. It knows what form to
// process it on based on the name 'payment-form'
jQuery(function ($) {
//payment submission
$('#ctl01').submit(function (event) {
alert('In .submit()');
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
//if there is a error, it is displayed on the page if there was
//no error this is where it gets sent to the server.
var stripeResponseHandler = function (status, response) {
var $form = $('#ctl01');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
});
</script>
所以JS創建條紋的道理,但我不知道如何得到這個打我的代碼隱藏我擊球的時候提交。該頁面閃爍,沒有任何內容。
我試圖讓按鈕成爲一個事件綁定到一個asp.net按鈕,沒有好處。
我應該在Page_Load()中做些什麼,以便將POST中的這些數據放到頁面中?
你可以擴展一點@mshsayem嗎?我相信我以前見過,但我不知道如何實現它,因爲我需要我的JavaScript執行第一,然後結果發送到代碼隱藏。 – ledgeJumper