Jquery AJAX提交表单错误

时间:2015-08-03 10:27:07

标签: jquery ajax forms expressionengine

我使用表达式引擎的插件自由格式创建了一个基本的提交表单。从CMS方面来看,我的数据正被提交到EE CMS中。但是,在我提交了我的详细信息后,我收到了object object表单上的以下消息,而不是收到成功消息。 <div id="form-messages" class="success">[object Object]</div>

我假设这与我的jquery有关因为当我通过php验证提交时函数正在工作。

以下是我的代码片段。

var form = $('#ajax-contact');
var formMessages = $('#form-messages');

$(form).submit(function(e) {

e.preventDefault();
var formData = $(form).serialize();

// Submit the form using AJAX.
$.ajax({
    type: 'POST',
    url: $(form).attr('action'),
    data: formData
})
.done(function(response) {
    //  formMessages div has the 'success' class.
    $(formMessages).removeClass('error');
    $(formMessages).addClass('success');

    // Set the message text.
    $(formMessages).text(response);

    // Clear the form.
    $('#name').val('');
    $('#email').val('');
})
.fail(function(data) {
    // Make sure that the formMessages div has the 'error' class.
    $(formMessages).removeClass('success');
    $(formMessages).addClass('error');

    // Set the message text.
    if (data.responseText !== '') {
        $(formMessages).text(data.responseText);
    } else {
        $(formMessages).text('Oops! An error occured and your message could not be sent.');
    }
});

})

HTML

<div id="form-messages" class="success"></div>

exp:freeform:form 
            form_id="1"
            admin_notify="me@email.com"
            form:class="main-contact submit-fade ajax-form"
            form:id="ajax-contact"


        }

            <ul class="small-block-grid-2 medium-block-grid-2 hide-form">
               <li>
                  <label for="name">Name</label>
                  {freeform:field:first_name
                      attr:class="form-control" 
                      attr:placeholder="First Name" 
                      attr:class="required"
                  }

                </li>

                <li>
                  <label for="email">Email</label>
                  {freeform:field:email
                      attr:class="form-control" 
                      attr:placeholder="Email" 
                      attr:class="required"
                  }
                 </li>
              </ul>
              <input type="submit" class="btn btn-success">

        {/exp:freeform:form}

1 个答案:

答案 0 :(得分:1)

响应被解析为JSON。使用:

.done(function(response) {
    if (response.success) {
        formMessages.removeClass("error").addClass("success").text("Success!");
        $("#name,#email").val("");
    } else {
        formMessages.removeClass("success").addClass("error").text("Oops, failure!");
    }
})