javascript - 使用一个输入字段提交 HTML 表单返回 : Uncaught TypeError: Converting circular structure to JSON

标签 javascript html json forms

无法弄清楚是什么导致了 Uncaught TypeError:将循环结构转换为 JSON。我有表单(1 个输入字段和提交按钮)

<form id="contact-form" method="post" action="submit_form.php" role="form" target="hiddenFrame">
<div class="controls">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="form_name">Author *</label>
                <input id="form_name" type="text" name="Author" class="form-control" placeholder="Enter name *" required="required" data-error="name is required.">
                <div class="help-block with-errors"></div>
            </div>
        </div>
    </div>
</div>
</div>
<div class="col-md-12 text-center">
    <input type="submit" class="btn btn-success btn-send" name="submit">
</div>
</div>
</div>
</form>
<script src="./js/submit_frm.js"></script>

submit_frm.js代码:

$(function () {

// init the validator
// validator files are included in the download package
// otherwise download from http://1000hz.github.io/bootstrap-validator

$('#contact-form').validator();


// when the form is submitted
$('#contact-form').on('submit', function (e) {

    // if the validator does not prevent form submit
    if (!e.isDefaultPrevented()) {
        var url = "submit_frm.php";

        // POST values in the background the the script URL
        var data = JSON.stringify(e);
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            success: function (data)
            {
                // data = JSON object that contact.php returns

                // we recieve the type of the message: success x danger and apply it to the 
                var messageAlert = 'alert-' + data.type;
                var messageText = data.message;

                // let's compose Bootstrap alert box HTML
                var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';

                // If we have messageAlert and messageText
                if (messageAlert && messageText) {
                    // inject the alert to .messages div in our form
                    $('#contact-form').find('.messages').html(alertBox);
                    // empty the form
                    $('#contact-form')[0].reset();
                }
            }
        });
        return false;
    }
})
});

Google Chrome 控制台显示,该错误源自 var data = JSON.stringify(e); js 文件中的行。如果您能帮助解决此错误,我将不胜感激。

最佳答案

这是无效的:var data = JSON.stringify(e);

事件中的 e 通常是对该事件的引用,而不是您的数据。你应该这样做:

e.preventDefault();

var data = $(this).serialize();

关于javascript - 使用一个输入字段提交 HTML 表单返回 : Uncaught TypeError: Converting circular structure to JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54736810/

相关文章:

json - PostgreSQL - 在不指定键的情况下聚合 JSON 对象中的值

android - 我无法使用 Volley 进行网络调用

javascript - 如何修复幻灯片导航

javascript - JQuery 切换删除

javascript - 如何获取 Vue.js 中计算数组的长度

HTML/CSS 最小宽度问题

html - 如何防止移动浏览器微缩 HTML block 级元素?

jquery - 使用 Jquery 将 css 应用于除一个元素之外的所有元素

javascript - 如何预填充下拉列表

java - GSON 解析器在循环 json 时跳过对象