Close.
$(document).ready(function() {
function ajax_start() {
alert("starting...");
}
$.ajax({
beforeSend: ajax_start, // <== remove the parens
url: "insert_part.php",
type:"POST",
data: "customer="+customer // <== as Skilldrick pointed out,
// remove the trailing comma as well
});
});
你需要这样做是因为
-
ajax_start()
评估为值returned通过执行名为的函数ajax_start
, but
-
ajax_start
评估为函数本身.
编辑回复:OP评论
“我如何在回调中包含第二个函数。类似 - beforesend: ajax_start,other_function (obv. 不完全一样)?”
有几种方法可以做到这一点。使用匿名函数将它们组合起来:
$.ajax({
// if you need the arguments passed to the callback
beforeSend: function (xhr, settings) {
ajax_start();
other_function();
},
url: "insert_part.php",
type:"POST",
data: "customer="+customer
});
或者只是声明一个执行您想要的操作的命名函数,然后使用它:
function combined_function(xhr, settings) {
ajax_start();
other_function();
}
$.ajax({
beforeSend: combined_function,
url: "insert_part.php",
type:"POST",
data: "customer="+customer
});