gpt4 book ai didi

javascript - 使用 AJAX 通过 Ruby on Rails 验证优惠券后获取数据

转载 作者:行者123 更新时间:2023-11-28 06:53:51 25 4
gpt4 key购买 nike

在我的 Rails 应用程序上,我需要验证优惠券并向用户提供有关他们将收到的折扣类型的反馈。

我已经能够使用 AJAX 成功检查优惠券是否有效。现在我正在尝试显示有关优惠券的数据,但到目前为止我的尝试尚未成功。

我有一个名为 Coupon 的模型,其中包含 codediscount 字段。

这是我的 Controller 代码:

class CouponsController < ApplicationController

def show
end

def check_coupon_code

if Coupon.exists?(:code => params[:coupon])
valid = true
@coupon = Coupon.find_by(:code => params[:coupon])
puts "Code Valid"
else
valid = false
puts "Code Invalid"
end
respond_to do |format|
if valid
format.json { render :json => @coupon }
else
format.json { }
end
return
end

end

end

表单上的 JavaScript:

<script>
$(document).ready(function(){
$('#subscription_stripe_coupon').change(function(){
coupon_code = $('#subscription_stripe_coupon').val();
$.ajax({
type: "POST",
url: "/coupons/check_coupon_code",
dataType: 'json',
data: { coupon : coupon_code},
success: function(data){
if (response) {
console.log("Valid Code!");
} else {
console.log("Invalid Code!");
}
}
});
});
});
</script>

我的路线:

post 'coupons/check_coupon_code' => 'coupons#check_coupon_code'

以下是无效优惠券的日志:

Started POST "/coupons/check_coupon_code" for 50.17.182.190 at 2015-09-23 03:02:35 +0000
Processing by CouponsController#check_coupon_code as JSON
Parameters: {"coupon"=>"asdf"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Coupon Exists (0.1ms) SELECT 1 AS one FROM "coupons" WHERE "coupons"."code" = 'asdf' LIMIT 1
Code Invalid
Rendered coupons/check_coupon_code.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 52ms (Views: 48.1ms | ActiveRecord: 0.6ms)

以下是有效优惠券的日志:

Started POST "/coupons/check_coupon_code" for 50.17.182.190 at 2015-09-23 03:19:21 +0000
Processing by CouponsController#check_coupon_code as JSON
Parameters: {"coupon"=>"STARTUPBROS2015"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
(0.1ms) BEGIN
SQL (1.6ms) UPDATE "users" SET "last_seen_at" = $1, "updated_at" = $2 WHERE "users"."id" = 1 [["last_seen_at", "2015-09-23 03:19:21.651990"], ["updated_at", "2015-09-23 03:19:21.653000"]]
(0.9ms) COMMIT
Coupon Exists (0.2ms) SELECT 1 AS one FROM "coupons" WHERE "coupons"."code" = 'STARTUPBROS2015' LIMIT 1
Coupon Load (0.2ms) SELECT "coupons".* FROM "coupons" WHERE "coupons"."code" = 'STARTUPBROS2015' LIMIT 1
Code Valid
Rendered coupons/check_coupon_code.html.erb within layouts/application (0.3ms)
Rendered layouts/_header.html.erb (0.9ms)
Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 58ms (Views: 47.9ms | ActiveRecord: 3.3ms)

最佳答案

尝试根据您的条件将response更改为data。然后,请将ajax更新为以下内容:

<script>
$('#subscription_stripe_coupon').change(function () {
coupon_code = $('#subscription_stripe_coupon').val();
$.ajax({
type: "POST",
url: "/coupons/check_coupon_code",
dataType: 'json',
data: {coupon: coupon_code}
}).done(function (data) {
console.log(data);
if (data) {
console.log("Valid Code!");
} else {
console.log("Invalid Code!");
}
}).fail(function () {
console.log("error");
}).always(function () {
console.log("complete");
});
});
</script>

关于javascript - 使用 AJAX 通过 Ruby on Rails 验证优惠券后获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730397/

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