jQuery Ajax没有返回成功 [英] jQuery Ajax not returning success

查看:45
本文介绍了jQuery Ajax没有返回成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码转到form.php,而不是在我的主页上将回显的php拉入#success div中:

I have this code that goes to form.php instead of pulling the echoed php into a #success div on my main page:

<script type="text/javascript">

    data = $(form).serialize();
    $('#form').submit(function() {   
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html('').html(result);
            }
        });
    });
</script>
                <div id="form">
                <br>
                <form action="http://www.mcfilmmakers.com/wp-content/themes/MC/form.php" method="post">
                    Name / Nom:<input type="text" name="fullname" /><br />
                    E-mail / Courriel:<input type="text" name="email" /><br />                          Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
                    Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
                    <input type="submit" value="Submit" />
                </form>
            </div>
            <div id="success">
            </div>

form.php确实收到了发布信息,因为它回显了输入.但是由于某种原因,它只是没有被插入到#success中.

The form.php does recieve the post information because it is echoing the input. For some reason though, it's just not being inserted into #success.

推荐答案

尝试一下:

$('#form').submit(function(event) {   
    $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
    });
    event.preventDefault();
});

提交表单时,默认情况下,它将在表单的action属性中发布表单.您想阻止该帖子的发生,而是使用ajax进行操作.因此 event.preventDefault() 可以防止这种情况.

When submitting your form, by default it will post the form the the action attribute of the form. You want to prevent that post from happening, and instead do it with ajax. So event.preventDefault() will prevent that.

也无需链接.html(''),因为它设置了整个内容.请参见 http://api.jquery.com/html/

Also no need to chain .html('') because it sets the entire content. See http://api.jquery.com/html/

这篇关于jQuery Ajax没有返回成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆