【问题标题】:Ajax form post issueAjax 表单发布问题
【发布时间】:2018-10-18 09:19:29
【问题描述】:

我正在使用 Godaddy 表单电子邮件 url 从托管页面中的表单发送电子邮件。表单将字段发布到“gdform.php”,它发送电子邮件并重定向到我的页面。它发布和重定向时没有任何问题。由于它再次刷新/加载页面,我想用 ajax 发布表单。当我使用 ajax 时,我无法收到任何电子邮件。我比较了请求和响应标头。现在我放了一些标题后,它们显示了相同的请求/响应数据。我可以从重定向页面内容的 ajax 请求中获得响应。这意味着表单成功发送到 gdform.php 并重定向到我的页面。 我不明白为什么 ajax 表单发布即使成功发布并获得回复也无法发送电子邮件。

// 联系表格

<form id="main-contact-form" name="contact-form" method="post" action="gdform.php">
<input type="text" name="name">
<input type="email" name="email">
<textarea name="comments" id="comments"></textarea>
<input type="submit" name="submit" value="Submit">

<input type="hidden" name="subject" value="Form Submission" />
<input type="hidden" name="redirect" value="index.html" />
<input type="hidden" name="form_order" value="alpha"/>
<input type="hidden" name="form_interval" value="default"/>
<input type="hidden" name="form_format" value="html"/>
</form>

//联系表单js

var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        headers: { "Upgrade-Insecure-Requests": "1",
                    Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        },
        crossDomain: true,
        url: $(this).attr('action'),
        beforeSend: function(){
            form.prepend(form_status.html('Email is sending...'));
        }
    }).done(function(data){
        console.log("done " + data);
        form_status.html('Thank you for contact us.');
    });
});

【问题讨论】:

    标签: jquery ajax forms post


    【解决方案1】:

    您的发布请求中似乎没有包含数据。

    在传递给 .ajax 的对象中包含“数据”属性:

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        headers: { "Upgrade-Insecure-Requests": "1",
                    Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        },
        crossDomain: true,
        url: $(this).attr('action'),
        data: someData, # <----------HERE
        beforeSend: function(){
            form.prepend(form_status.html('Email is sending...'));
        }
    })
    

    您需要从您要发送的 dom 元素中进行整理。我认为您可以使用 .serialize() 来做到这一点,例如:

    let someData = $('#main-contact-form').serialize();
    

    【讨论】:

    • 添加“数据:$(this).serialize()”解决了它。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 2015-11-11
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多