简体   繁体   English

AJAX回调函数不起作用

[英]AJAX callback functions not working

Let me preface this by saying I am extremely new to AJAX/JS in general. 首先,我通常对AJAX / JS陌生。 I have an issue with undefined return values from AJAX calls. 我对AJAX调用的未定义返回值有疑问。 I've tried following some online tutorials or other answers on stack overflow, but the whole callback function this is not working for me. 我尝试按照一些在线教程或有关堆栈溢出的其他答案进行操作,但是整个回调函数对我来说不起作用。 All i want is the data returned so I can use it elsewhere in my code (specifically plotting some charts). 我想要的只是返回的数据,因此我可以在代码的其他地方使用它(特别是绘制一些图表)。 Putting the plotting code within the success block seems tacky, so I would like to avoid doing that. 将绘图代码放在成功块中似乎很俗气,所以我想避免这样做。

I would really appreciate any help. 我真的很感谢您的帮助。 My code is below: 我的代码如下:

function updateMe(callback) {
    var count1;
    var PointsArray = chart.getSelectedPoints();
    var xArray = [];
    var yArray = [];
    var contributions = [];
    var finished = 0;

    for(count1 = 0; count1 < PointsArray.length; count1++){
        yArray[count1] = PointsArray[count1].id;
    };

    $.ajax({
        type: 'POST',
        url: "...",
        dataType: "json",
        data: {
            csrfmiddlewaretoken: '{{csrf_token}}',
            'data[]': (yArray),
        },
        success: function(data){
            callCallback(data, callback);
        }

    });
};

function myCallback(result) {
    return result;
}

function callCallback(data, cb){
    cb(data);
};

var toParse;
toParse = updateMe(myCallback);

EDIT: 编辑:

I should add that if i do 我应该补充一点

x = updateMe(myCallback)

in the console, I get the desired result. 在控制台中,我得到了预期的结果。 Then I can use x.responseText to extract the individual data. 然后,我可以使用x.responseText提取单个数据。 However if I do 但是如果我这样做

x = updateMe(myCallback).responseText

I get undefined 我不确定

EDIT #2 Why this is not a duplicate 编辑#2为什么这不是重复的

The tagged question talks about using callbacks as a way to get deal with the asynchronous nature of AJAX calls. 标记的问题是关于使用回调作为处理AJAX调用的异步特性的一种方法。 My question asks why my callback functions are not working as expected. 我的问题问为什么我的回调函数无法按预期工作。

I'm not sure why you need that callback function passed into your ajax method. 我不确定为什么需要将回调函数传递到ajax方法中。 Wouldn't it be more intuitive to have your code be: 编写代码是不是更直观:

function callCallback(data){
  console.log(data.responseText);
};

That way there's no need to pass it into the first function, and you can call/store the data immediately rather than handing it off to a seemingly redundant function. 这样就无需将其传递给第一个函数,您可以立即调用/存储数据,而不必将其交给看似冗余的函数。 You're trying to call the responseText from the function itself immediately, which won't work as the asynchronous callback hasn't finished yet. 您正在尝试立即从函数本身调用responseText,因为异步回调尚未完成,因此该函数无法正常工作。

[EDIT] This response --to a question of which yours is possibly a duplicate--is very in-depth, and gives a good summary of what is happening with asynchronous requests and also how to handle them based on different ES standards. [编辑] 这个回答 (对于您的问题可能是重复的)非常深入,并且很好地总结了异步请求的情况以及如何根据不同的ES标准处理它们。

Try changin callCalback function as follows; 如下尝试changin callCalback函数;

cb.apply(this, data) cb.apply(此数据)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM