无法获得成功回调

无法获得成功回调

本文介绍了用jquery、ajax提交表单,无法获得成功回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经很接近了,但我需要一些帮助来克服困难.我正在尝试通过 AJAX 提交表单,在 Rails 应用程序中发送电子邮件,然后在成功时执行一些代码.当我在表单上按 Enter 键时,AJAX 正在运行,因为我收到了电子邮件.但是,成功代码没有运行(也没有运行错误).我也试过完整:"但没有用,所以我怀疑默认表单行为可能有问题?想法?

这里是ajax:

//AJAX 函数,无需刷新页面即可提交邮件表单$("#contact_me").submit(function(){var dataSet = $(this).serialize();$.ajax({类型:POST",网址:$(this).attr("action"),数据:数据集,成功:函数(){警报(已发送!");},错误:函数(){alert("出了点问题!");}});返回假;})

以及它调用的 rails 操作:

def email_me名称 = 参数 [:名称]电子邮件 = 参数 [:电子邮件]内容=参数[:文本]ContactMailer.first_contact(name, email, content).deliver头:好的结尾

还有表单本身

<div><p><%= label_tag(:name, "Name") %><%= text_field_tag(:name) %></p>

<div><p><%= label_tag(:email, "Email") %><%= email_field(:user, :email)%></p>

<div><p><%= label_tag(:text, "你在想什么?") %></p>

<%= submit_tag("问好!")%><%结束%>

解决方案

最终导致最大问题的是小事.我忘了将整个函数包装在 function(){..} 中.去图,现在效果很好.感谢大家的帮助.

$(function(){$("#contact_me").submit(function(){var dataSet = $(this).serialize();$.ajax({类型:POST",网址:$(this).attr("action"),数据:数据集,完成:功能(){警报(已发送!");},错误:函数(){alert("出了点问题!");}});返回假;});})

Alright, I'm pretty close on this, but I need a bit of help getting over the hump. I'm trying to submit a form via AJAX, in a rails app, which sends an email, and then executes some code on success. When I hit enter on my form, the AJAX is working, because I'm getting the email. However, the success code isn't running (nor the error for that matter). I also tried 'complete:' but that didn't work, so I suspect it may be some problem with the default form behavior? Ideas?

Here is the ajax:

// AJAX Function to submit email form without refreshing page
$("#contact_me").submit(function(){
    var dataSet = $(this).serialize();
    $.ajax({
        type: "POST",
        url: $(this).attr("action"),
        data: dataSet,
        success: function(){
            alert("Sent!");
        },
        error: function(){
            alert("Something went wrong!");
        }
    });

    return false;
})

And the rails action which it calls:

def email_me
    name = params[:name]
    email = params[:email]
    content = params[:text]

    ContactMailer.first_contact(name, email, content).deliver

    head :ok
end

Also, the form itself

<%= form_tag({controller: "main", action: "email_me"}, id: "contact_me", remote: "true") do %>
    <div>
        <p><%= label_tag(:name, "Name") %>
        <%= text_field_tag(:name) %></p>
    </div>
    <div>
        <p><%= label_tag(:email, "Email") %>
        <%= email_field(:user, :email) %></p>
    </div>
    <div>
        <p><%= label_tag(:text, "What's on your mind?") %></p>
        <%= text_area_tag(:text)%>
    </div>

    <%= submit_tag("Say Hello!")%>
<% end %>
解决方案

It's really the little things that end up causing the biggest problems. I forgot to wrap the whole function in a function(){..}. Go figure, works great now. Thanks for the help all.

$(function(){
    $("#contact_me").submit(function(){
        var dataSet = $(this).serialize();
        $.ajax({
            type: "POST",
            url: $(this).attr("action"),
            data: dataSet,
            complete: function(){
                alert("Sent!");
            },
            error: function(){
                alert("Something went wrong!");
            }
        });
        return false;
    });
})

这篇关于用jquery、ajax提交表单,无法获得成功回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:33