ajax:success 未触发 ajax rails 4 个应用
ajax:success not triggering ajax rails 4 app
我在 rails 应用程序中有 jQuery 函数,我正试图从中进行 ajax 调用。
问题是它没有返回 ajax 响应,但它发送 ajax 调用成功。
我需要将我的 ajax 调用与该函数绑定,这样我才能显示响应。
jQuery代码:
$(document).ready(function () {
$("#contact_us").bind("ajax:success", function (e, data, status, xhr) {
if (data.success) {
$("#alert_success").show();
$(".CaptchaError").html(data.message);
} else {
$("#alert_failed").show();
$(".CaptchaError").html(data.message);
}
});
});
Rails代码:
class MailController < ApplicationController
prepend_before_action :check_captcha
def contact_us
full_name = params['contact_full_name']
last_name = params['contact_last_name']
phone = params['contact_phone']
website = params['contact_website']
email = params['contact_email']
message = params['contact_message']
ContactMailer.send_email(full_name, last_name, phone, website, email, message)
end
private
def check_captcha
unless verify_recaptcha
return render json: {:success => false, :message => "You cant use this captcha code"}
else
contact_us
return render json: {:success => true, :message => "Your email has been sent successfully"}
end
end
end
Rails 形式:
<%=form_tag contact_us_mail_index_path, :id => "contact_us",
:format => :json, :remote => true do %>
<%end%>
按钮代码:
<button type="submit" class="btn btn-lg btn-default"><i class="fa
fa-check"></i> SEND MESSAGE</button>
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require plugins/jquery/jquery-2.2.3.min.js
//= require scripts
//= require plugins/slider.revolution/js/jquery.themepunch.tools.min
//= require plugins/slider.revolution/js/jquery.themepunch.revolution.min
//= require view/demo.revolution_slider
//= require plugins/smoothscroll
//= require contact
routes.rb
resources :mail, except: [:show,:new,:create,:edit,:update,:destroy] do
collection do
post :contact_us
end
end
我认为你应该创建一个文件contact_us.js.erb
contact_us.js.erb
if ("<% @success %>")
$("#alert_success").show();
else
$("#alert_failed").show();
$(".CaptchaError").html("<% @msg %>");
表格
删除format: json
<%=form_tag contact_us_path, :id => "contact_us", :remote => true do %>
<%end%>
HomeConrtoller
def contact_us
respond_to do |format|
if verify_recaptcha
full_name = params['contact_full_name']
last_name = params['contact_last_name']
phone = params['contact_phone']
website = params['contact_website']
email = params['contact_email']
message = params['contact_message']
ContactMailer.send_email(full_name, last_name, phone, website, email, message)
@success = true
@msg = "Your email has been sent successfully"
format.js
else
@success = false
@msg = "You cant use this captcha code"
format.js
end
end
end
路线
post '/contact_us' => 'home#contact_us', as: :contact_us
希望对您有所帮助。
经过多次尝试,我终于解决了这个问题。
问题与 turbolinks 有关。根据我的研究,问题出在 application.js 文件中。
我做了一些更改,发现 turbolinks js 正在创建问题。
所以,我安装了一个 gem 名为:jquery-turbolinks
安装后,我的application.js是这样的:
//= require plugins/jquery/jquery-2.2.3.min
//= require jquery.turbolinks
//= require jquery_ujs
//= require scripts
//= require contact
发布我的答案 确实对我有很大帮助,但还不足以为我提供方向和修复代码。
我在 rails 应用程序中有 jQuery 函数,我正试图从中进行 ajax 调用。
问题是它没有返回 ajax 响应,但它发送 ajax 调用成功。
我需要将我的 ajax 调用与该函数绑定,这样我才能显示响应。
jQuery代码:
$(document).ready(function () {
$("#contact_us").bind("ajax:success", function (e, data, status, xhr) {
if (data.success) {
$("#alert_success").show();
$(".CaptchaError").html(data.message);
} else {
$("#alert_failed").show();
$(".CaptchaError").html(data.message);
}
});
});
Rails代码:
class MailController < ApplicationController
prepend_before_action :check_captcha
def contact_us
full_name = params['contact_full_name']
last_name = params['contact_last_name']
phone = params['contact_phone']
website = params['contact_website']
email = params['contact_email']
message = params['contact_message']
ContactMailer.send_email(full_name, last_name, phone, website, email, message)
end
private
def check_captcha
unless verify_recaptcha
return render json: {:success => false, :message => "You cant use this captcha code"}
else
contact_us
return render json: {:success => true, :message => "Your email has been sent successfully"}
end
end
end
Rails 形式:
<%=form_tag contact_us_mail_index_path, :id => "contact_us",
:format => :json, :remote => true do %>
<%end%>
按钮代码:
<button type="submit" class="btn btn-lg btn-default"><i class="fa
fa-check"></i> SEND MESSAGE</button>
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require plugins/jquery/jquery-2.2.3.min.js
//= require scripts
//= require plugins/slider.revolution/js/jquery.themepunch.tools.min
//= require plugins/slider.revolution/js/jquery.themepunch.revolution.min
//= require view/demo.revolution_slider
//= require plugins/smoothscroll
//= require contact
routes.rb
resources :mail, except: [:show,:new,:create,:edit,:update,:destroy] do
collection do
post :contact_us
end
end
我认为你应该创建一个文件contact_us.js.erb
contact_us.js.erb
if ("<% @success %>")
$("#alert_success").show();
else
$("#alert_failed").show();
$(".CaptchaError").html("<% @msg %>");
表格
删除format: json
<%=form_tag contact_us_path, :id => "contact_us", :remote => true do %>
<%end%>
HomeConrtoller
def contact_us
respond_to do |format|
if verify_recaptcha
full_name = params['contact_full_name']
last_name = params['contact_last_name']
phone = params['contact_phone']
website = params['contact_website']
email = params['contact_email']
message = params['contact_message']
ContactMailer.send_email(full_name, last_name, phone, website, email, message)
@success = true
@msg = "Your email has been sent successfully"
format.js
else
@success = false
@msg = "You cant use this captcha code"
format.js
end
end
end
路线
post '/contact_us' => 'home#contact_us', as: :contact_us
希望对您有所帮助。
经过多次尝试,我终于解决了这个问题。
问题与 turbolinks 有关。根据我的研究,问题出在 application.js 文件中。
我做了一些更改,发现 turbolinks js 正在创建问题。
所以,我安装了一个 gem 名为:jquery-turbolinks
安装后,我的application.js是这样的:
//= require plugins/jquery/jquery-2.2.3.min
//= require jquery.turbolinks
//= require jquery_ujs
//= require scripts
//= require contact
发布我的答案