简体   繁体   English

通过回调从异步函数返回数据时未定义

[英]Returning data from asynchronous function through callback comes as undefined

The function gets the data from URL and then passes it to another function where the listing is done dynamically based on users in the list of URL. 该函数从URL获取数据,然后将其传递给另一个函数,在该函数中,将根据URL列表中的用户动态完成列表。 I tried callback but I am getting the following error service.js:9 Uncaught TypeError: callback is not a function 我尝试了回调,但收到以下错误service.js:9 Uncaught TypeError: callback is not a function

This is the function in one js file: 这是一个js文件中的函数:

function GetData(callback, passdata) {
    $.ajax({
        type: 'GET',
        url: 'https://jsonplaceholder.typicode.com/users',
        success: function (response) {
            debugger;
            console.log(response);
            return callback(response, passdata);
        }
    });
 } 

This is the function in another js file (wherein I want to list the data from the URL): 这是另一个js文件中的函数(我要在其中列出URL中的数据):

$(document).ready(function () {

        var getData = GetData();
        var $data = $('#dataDisplay');

        function listData(response, passdata) {
            var data = response;
            var passeddata = passdata;

            $.each(data, function (i, users) {
                $data.append('<li>' + '<span>' + users.name + '</span>' + '<br> <span>' + users.email + '</span>' + ' </li>');
            });

            //adds li dynamically 
            $("li").append('<i class="material-icons delete">' + "delete" + '</i>');
            $("li").append('<i class="material-icons edit">' + "edit" + '</i>');

    }
}); 

You can use anonymous function in document ready => 您可以在准备就绪的文档中使用匿名函数=>

GetData(function(result){
// can do further things here..
console.log(result);

}, passdata);

this should fix your error. 这应该可以解决您的错误。

For me this worked 对我来说这很有效

 function GetData(callback) { debugger; $.ajax({ type: 'GET', url: ' http://localhost:3000/users', success: function (response) { console.log(response); callback(response); } }); } 

In another js file call the function back and pass the response parameter for that is where the array of the API was saved. 在另一个js文件中,调用该函数并传递响应参数,该参数是保存API数组的位置。

 GetData(function (response) { debugger; var data = response; var $data = $('#dataDisplay'); $.each(data, function (i, users) { $data.append('<li>' + '<span class="table .table-striped .table-hover">' + users.first_name + '</span>' + ' <span class="table .table-striped .table-hover">' + users.email + '</span>' + ' </li>'); }); //adds li dynamically $("li").append('<i class="material-icons delete ">' + "delete" + '</i>'); $("li").append('<i class="material-icons edit ">' + "edit" + '</i>'); }); }); 

How can JavaScript know that listData is the callback function if you don't specify it? 如果您不指定listData JavaScript怎么能知道listData是回调函数?

You just declared the listData function, but you aren't using it anywhere. 您刚刚声明了listData函数,但没有在任何地方使用它。 There is not any sort of magic that will do it for you :) 没有任何一种魔法可以帮到你:)

Just change 只是改变

var getData = GetData();

To

var getData = GetData(listData, 'something');

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

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