调试jquery处理程序

Debugging jquery handlers

本文关键字:程序 处理 jquery 调试      更新时间:2023-09-26

这个问题是这个问题的后续问题。我创建了一个简单的示例来检查代码是如何在处理程序中执行的。对于表单

<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);
    });
});

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

编辑:

代码在firefox中不能正常工作。

至少,您正在调用一个在您发布的代码中没有定义的函数get_form_data_with_token()。也许你想叫你的get_form_data_uid()

本想把这句话加个注释,但显然不能。