0

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.

Community
  • 1
  • 1
Sebi
  • 4,262
  • 13
  • 60
  • 116

1 Answers1

0

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.

Thernys
  • 723
  • 4
  • 13
  • I've made a mistake when copying the code. The alert isn't shown regardless. – Sebi Oct 13 '15 at 22:34
  • All right, indeed your fiddle seems to fail with an error even after I fix the function name. However, when I then copy the corrected code verbatim to this Codepen http://codepen.io/anon/pen/BodXRp , it does in fact show the alert as expected. – Thernys Oct 13 '15 at 22:41
  • Oh, your fiddle (after the fix) only failed because you had not included jQuery as a library, but use it in your code. After these two fixes, the fiddle does show the alert. ( https://jsfiddle.net/8g0rvgnt/7/ ) – Thernys Oct 13 '15 at 22:48
  • It's working on jsfiddle but not in my browser. I have no idea why. – Sebi Oct 14 '15 at 12:47