我试图重写我的AJAX请求,以便我可以调试它的响应,所以我将代码从响应转移到单个函数中。在我的原始代码中,我能够从AJAX调用返回结果,并在成功响应中输出它。
由于将该代码移动到单独的函数中然后尝试在成功响应中调用该函数,我得到错误'结果未定义'。我不熟悉JavaScript,知道为什么会这样,请帮忙。
$.ajax({
type: 'GET',
url: '/api/auth/api/Homepage/GetLinks',
success:displayLinks(result, jqStatus, error),
error: showPolicyLinkError(result, jqStatus, error)
});
function displayLinks(result, jqStatus, error){
$('#numbercontrol').html(result);
console.log("Success Log - Satus:" + jqStatus + " Error:" + error);
}
function showLinkError(result, jqStatus, error){
$('#numbercontrol').html("<p>Unable to return any links.</p>");
console.log("Failure Log - Result: " + result + "Satus:" + jqStatus + " Error:" + error);
}
答案 0 :(得分:3)
您应该只传递不带参数的函数名称:
$.ajax({
type: 'GET',
url: '/api/auth/api/Homepage/GetLinks',
success: displayLinks,
error: showLinkError
});