新手到JQuery / JSON / AJAX所以请你好。
我在SO和其他网站上拼凑了这些艺术作品,但我很挣扎。
我已经创建了一些处理AJAX响应的函数......
function newOrderSuccess(response) { ... }
function newOrderTimeout() { ... }
function newOrderFail() { ... }
...
以下是AJAX调用:
function sendCallAjaxUsingJson(theUrl, theData, successCallbackFunction, timeoutCallbackFunction, otherErrorCallback, timeoutValueMilli)
{
var successFn = successCallbackFunction;
var timeoutFn = timeoutCallbackFunction;
var otherFn = otherErrorCallback;
if(!(typeof successFn === 'function') || !(typeof timeoutFn === 'function') || !(typeof otherFn === 'function'))
return false;
$.ajax({
type: "POST",
url: theUrl,
timeout:timeoutValueMilli,
dataType: 'json',
data: { json: JSON.stringify(theData) },
success:successFn(result),
error: function(x, t, m) {
if(t==="timeout") {
timeoutFn();
} else {
otherFn();
}
}
});
}
我的代码调用函数如下:
sendCallAjaxUsingJson("/ordertaker.php", 'submitOrder','newOrderSuccess', 'newOrderTimeout', 'newOrderFail',1000);
结果是.....没什么。在我上传newOrderFail()
文件之前,我正在使用ordertaker.php
函数,但现在我什么也没得到。
我哪里出错了?
答案 0 :(得分:1)
您正在将字符串传递给sendCallAjaxUsingJson
而不是函数,
sendCallAjaxUsingJson("/ordertaker.php", 'submitOrder',newOrderSuccess, newOrderTimeout, newOrderFail,1000);
此外,您在ajax调用中调用了您的成功函数而不是设置它。
success:successFn,