【问题标题】:Debugging jquery handlers调试 jquery 处理程序
【发布时间】:2015-10-13 22:03:00
【问题描述】:

这个问题是this one 的后续问题。我创建了一个simple example 来检查代码是如何在处理程序中执行的。对于表格

<form id="calendar_id" method="post">
    Insert date: <input id="date_id" type="text" name="l_date" required>
</form>

我正在尝试使用以下 javascript 检索字段:

function get_form_data_uid($form) {
    var unindexed_array = $form.serializeArray();
    var indexed_array = {};

    $.map(unindexed_array, function (n, i) {
        indexed_array[n['name']] = n['value'];
    });

    indexed_array['uid'] = 'badbfadbbfi';

    return indexed_array;
}

$("#calendar_id").submit(function (e) {
    var uri, method, formId, $form, form_data;
    // Prevent default submit
    e.preventDefault();
    e.stopImmediatePropagation();

    uri = "/";
    method = "POST";
    formId = "#calendar_id";

    $form = $(formId);
    form_data = get_form_data_uid($form);

    alert("form_data " + form_data);

    // Set-up ajax call
    var request = {
        url: uri,
        type: method,
        contentType: "application/json",
        accepts: "application/json",
        cache: false,
        // Setting async to false to give enough time to initialize the local storage with the "token" key
        async: false,
        dataType: "json",
        data: form_data
    };
    // Make the request
    $.ajax(request).done(function (data) { // Handle the response
        // Attributes are retrieved as object.attribute_name
        console.log("Data from change password from server: " + data);
        alert(data.message);
    }).fail(function (jqXHR, textStatus, errorThrown) { // Handle failure
        console.log(JSON.stringify(jqXHR));
        console.log("AJAX error on changing password: " + textStatus + ' : ' + errorThrown);
    });

});

但是,处理程序中的代码未执行(未显示警报)。为什么?

编辑:

代码在 jsfiddle 中有效,但在 Firefox 中无效。

【问题讨论】:

    标签: javascript jquery html ajax forms


    【解决方案1】:

    至少,您正在调用一个函数get_form_data_with_token(),该函数未在您发布的代码中的任何地方定义。也许您的意思是打电话给您的get_form_data_uid()

    本来想发表评论的,但显然不能。

    【讨论】:

    • 我复制代码时出错了。无论如何都不会显示警报。
    • 好吧,即使我修复了函数名,你的小提琴似乎也因错误而失败。但是,当我将更正后的代码逐字复制到此 Codepen codepen.io/anon/pen/BodXRp 时,它实际上确实按预期显示了警报。
    • 哦,您的小提琴(修复后)只是失败了,因为您没有将 jQuery 作为库包含,而是在您的代码中使用它。在这两个修复之后,小提琴确实显示了警报。 (jsfiddle.net/8g0rvgnt/7)
    • 它在 jsfiddle 上运行,但不在我的浏览器中。我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多