gpt4 book ai didi

javascript - Ajax调用返回函数而不是数据

转载 作者:行者123 更新时间:2023-12-02 23:17:25 26 4
gpt4 key购买 nike

我使用 jquery $ajax 调用发出请求,但它返回回调函数本身,而不是正确的数据响应,并显示消息“ReferenceError:recapCB 未定义”;

var recapMonthDEF = $.ajax({
type: 'get',
url: getAnalyticsApiURL('recapMonth'),
dataType: 'jsonp',
contentType: "application/json",
});

$.when(lastMonthSessionsDEF, recapMonthDEF).always(function(){
$('#Dashboard-tabs-1 .toggle input[type="checkbox"]').removeAttr('disabled');
});

recapMonthDEF.always(recapCB);



var recapCB = function(response){

if($('#newUsers').length == 0) return;

data = response;

var percent = parseFloat(parseFloat(data.percentNewSessions).toFixed(1));
var percentDiff = parseFloat((100 - percent).toFixed(1));

var sessionDuration = moment().startOf('day').seconds(data.sessionDuration).format('H:mm:ss');

$('#analitycs-session').html(data.sessions);
$('#analitycs-users').html(data.users);
$('#analitycs-pageviews').html(data.pageviews);
$('#analitycs-sessionsPerUser').html(parseFloat(data.sessionsPerUser).toFixed(2));
$('#analitycs-sessionDuration').html(sessionDuration);
$('#analitycs-newUsers').html(data.newUsers);

};

截图 Screenshot

最佳答案

but it returns the callback function itself

您的屏幕截图未显示回调函数。它显示了一个 jqXHR 对象。

请参阅documentation :

jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { }); (added in jQuery 1.6)
An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.

In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

由于您正在获取 jqXHR 对象,因此您必须触发错误状态。

这很可能是因为您使用的 URL 未提供 JSONP 响应。

<小时/>

ReferenceError: recapCB is not defined

您在尝试使用之后行上定义recapCB

关于javascript - Ajax调用返回函数而不是数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57112566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com