简体   繁体   English

Ajax表单重新加载页面,而不是在后台工作

[英]Ajax form reloading page instead of working in the background

Firstly, based on other suggestions, I've tried moving around the preventDefault() but haven't had success. 首先,根据其他建议,我尝试绕过了preventDefault()但没有成功。

My feedback form uses this JavaScript to pass form fields to "process.php" and return with the relevant messages (ie: "sucesss" or "fail") 我的反馈表单使用此JavaScript将表单字段传递给“ process.php”并返回相关消息(即:“成功”或“失败”)

It's doing its job except that instead of staying on the same page 'feedback.php' it loads 'process.php' page with this ... {"Data":"Data Value"} 它正在执行其工作,除了不是将其停留在同一页面“ feedback.php”上,而是使用以下方法加载“ process.php”页面... {“ Data”:“ Data Value”}

Heres the code: 这是代码:

$(document).ready(
function(){
    $('form').submit(
        function(event){
            var formData =
            {
                'name' : $('input[name=name]').val(),
                'page' : $('input[name=page]').val()
            };

            $('.form-group').removeClass('has-error'); // remove the error class
            $('.error').remove(); // remove the error text

            event.preventDefault();

            $.ajax({
                    type     : 'POST', // define the type of HTTP verb we want to use (POST for our form)
                    url      : 'process.php', // the url where we want to POST
                    data     : formData, // our data object
                    dataType : 'json', // what type of data do we expect back from the server
                    encode   : true
                })

            .done(function(data){
                    if ( ! data.success)
                    {
                        if (data.errors.name)
                        {
                            $('#name-group').addClass('has-error'); // add the error class to show red input
                            $('#nameField').append(data.errors.name); // add the actual error name under our input
                        }
                    }
                    else 
                    {
                        $('form').append('<div class="buttonError">Message Sent!</div>');
                        form.myButton.disabled = true;
                    }
                }
            );

            .fail(function(data){
                    $('form').append('<div class="buttonError">Failed!</div>');
                }
                console.log(data);
            );
            event.preventDefault();
        }
    );
});

looks like syntax error calling $.ajax, it should be corrected like this: 看起来像调用$ .ajax的语法错误,应该像这样进行更正:

    $.ajax({
        type     : 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url      : 'process.php', // the url where we want to POST
        data     : formData, // our data object
        dataType : 'json', // what type of data do we expect back from the server
        encode   : true
    }).done(function(data){
        if ( ! data.success)
        {
            if (data.errors.name)
            {
                $('#name-group').addClass('has-error'); // add the error class to show red input
                $('#nameField').append(data.errors.name); // add the actual error name under our input
            }
        }
        else 
        {
            $('form').append('<div class="buttonError">Message Sent!</div>');
            form.myButton.disabled = true;
        }
    }).fail(function(data){
        $('form').append('<div class="buttonError">Failed!</div>');
        console.log(data);
    });

Remove the method and action in the form tag in your HTML file. 删除HTML文件中form标记中的方法和操作。 This looks fine. 看起来不错。 Let me know if it doesn't work. 让我知道它是否无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM