我正在制作一个Rails应用程序。我的视图向我的服务器发送一个Ajax POST请求和一个电话号码。我在控制器中使用该电话号码调用一个model方法,该方法使用Twilio API向该号码发送sms。
问题是,即使Twilio API成功发送,我视图中的AJAX成功函数也没有运行。是什么导致了这个问题?
视图如下所示:
<% content_for(:after_js) do %>
<%= javascript_tag do %>
$(document).ready(function() {
$('form').submit(function() {
var phoneNumber = $(this).serialize();
var submit = $(this).find(":submit");
submit.attr("disabled", true);
$.ajax({
type: "POST",
url: $(this).attr('action'), //sumbits it to the given url of the form
data: phoneNumber,
dataType: "JSON"
}).success(function(json){
console.log("success", json);
submit.css("background-color", "green");
submit.val("success");
});
return false; // prevents normal behaviour
});
});
<% end %>
<% end %>
<%= form_tag(invite_with_twilio_reservation_path(@reservation, token: @reservation.token), method: "post") do %>
<%= label_tag(:number, "Send to:") %>
<%= text_field_tag(:number) %>
<%= submit_tag("send")%>
<% end %>下面是控制器:
def invite_with_twilio
@reservation = Reservation.find(params[:id])
if params[:token] == @reservation.token
authorize @reservation
else
raise
end
number = params[:number]
status = @reservation.send_sms_invitation_to_number(number, current_user)
if status == true
render json: number, status: :ok
else
render json: number, status: :fail
end
end这是一个模型:
def send_sms_invitation_to_number(number, user)
club_name = tables.first.club.name
account_sid = ENV['TWILIO_API_ACCOUNT_SID']
auth_token = ENV['TWILIO_API_AUTH_TOKEN']
Twilio.configure do |config|
config.account_sid = account_sid
config.auth_token = auth_token
end
@client = Twilio::REST::Client.new
if @client.messages.create(
from: "+13238005977" ,
to: "#{number}",
body: "Hey! Your buddy #{user.full_name}, wants to invite you to a reservation in #{club_name}, on the
#{date}. Follow this link to accept his invitation and coordinate with your friends. #{Rails.application.routes.url_helpers.reservation_url(self, token: self.token, host: 'localhost')}.")
return true
else
return false
end
end同样,sms发送成功。问题是AJAX成功函数没有被调用,按钮既不改变文本也不改变颜色。
发布于 2017-06-28 23:50:56
尝试这样做:
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: phoneNumber,
dataType: "JSON",
success: function(json){
// SMS success
},
error: function(json) {
// SMS not sent
}
});我希望这对你有帮助!
发布于 2017-06-29 00:37:44
Twilio开发者的布道者在这里。
您的问题似乎出在您的$.ajax函数中。Julien已经提出了一个修复它的方法,但我想我只是想找出问题所在来帮助解决这个问题。你的代码是:
$.ajax({
type: "POST",
url: $(this).attr('action'), //sumbits it to the given url of the form
data: phoneNumber,
dataType: "JSON"
}).success(function(json){
console.log("success", json);
submit.css("background-color", "green");
submit.val("success");
});因此,您是根据调用$.ajax的结果调用success的。根据jQuery文档,$.ajax返回jqXHR类型的对象。此对象用于响应success、error和complete,但它们已被弃用,并作为jQuery 3的一部分被删除。
相反,你可以按照朱利安的建议,在传递给$.ajax的选项对象中使用success键。或者,您可以使用done作为success的替代品。
$.ajax({
type: "POST",
url: $(this).attr('action'), //sumbits it to the given url of the form
data: phoneNumber,
dataType: "JSON"
}).done(function(json){
console.log("success", json);
submit.css("background-color", "green");
submit.val("success");
});不要忘了实现fail,这样你就可以处理失败了。
https://stackoverflow.com/questions/44807154
复制相似问题