Debugging jquery handlers

2019-06-14 12:02发布

This question is a followup of this one. I have created a simple example to check how code is executed within the handler. For the form

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

I'm trying to retrieve the fields using the following 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);
    });

});

However, the code within the handler is not executed (the alert is not shown). Why?

Edit:

The code works jsfiddle but not in firefox.

1条回答
Juvenile、少年°
2楼-- · 2019-06-14 12:34

At least, you are calling a function get_form_data_with_token() which is not defined anywhere in your posted code. Perhaps you meant to call your get_form_data_uid().

Would have just made this a comment, but apparently cannot.

查看更多
登录 后发表回答