调试jQuery的处理程序 [英] Debugging jquery handlers
问题描述
此问题是<一后随href="http://stackoverflow.com/questions/33110233/$p$pventing-jquery-ajax-call-from-sending-form-data-twice">this一。我创建href="https://jsfiddle.net/8g0rvgnt/5/" rel="nofollow">简单的例子来检查有code是处理程序中执行的
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>
我尝试使用下面的JavaScript来检索字段:
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);
});
});
然而,不执行该处理程序中的code(警报未示出)。为什么?
However, the code within the handler is not executed (the alert is not shown). Why?
编辑:
在code工作的jsfiddle 但不是在Firefox浏览器。
The code works jsfiddle but not in firefox.
推荐答案
至少,你调用一个函数 get_form_data_with_token()
这是没有定义在贴$随时随地C $℃。也许你的意思是打电话给你的 get_form_data_uid()
。
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.
这篇关于调试jQuery的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!