简体   繁体   English

如果重构,则无法重构AJAX调用

[英]Refactoring an AJAX call doesn't work if refactored

This is what the code looked like before: 这是之前的代码:

function getData(query) {
    $.ajax({
        url: "/Api/GetData/" + query,
            success: function (data) {
                var template = $dataTemplate.html();
                var html = Mustache.to_html(template, data);
                $dataField.html(html);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
            }
        });
}

I use this to bind the data to a select field and it work perfectly, but now I will need the same code in a different place, so I decided to separate the call in a different file so I can reuse it. 我使用它将数据绑定到选择字段,并且可以完美地工作,但是现在我将在不同的地方使用相同的代码,因此我决定将调用分隔在不同的文件中,以便可以重用它。 This is the refactored AJAX call: 这是重构的AJAX调用:

function getDataApiCall(query) {
    $.ajax({
        url: "/Api/GetData/" + query,
            success: function (data) {
                console.log(data);
                return data;
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.responseText);
                return null;
            }
        });
}

And now this is what the first function looks like: 现在,这是第一个函数的样子:

function getData(query) {
    var data = getDataApiCall(query);
    if (data != null)
    {
        console.log(data);
        var template = $dataTemplate.html();
        var html = Mustache.to_html(template, data);
        $dataField.html(html);
    }
}

The first console log is undefined and the second one looks OK, has all the data there, but also has a __proto__ element that I have no idea why is there. 第一个控制台日志未定义,第二个看起来不错,所有数据都在那里,但是还有一个__proto__元素,我不知道为什么在那里。 Because the array returned isn't null, the select gets bound but for some reason it's empty. 因为返回的数组不为空,所以选择被绑定,但是由于某种原因它为空。 Any idea where I'm going wrong? 知道我哪里出错了吗?

Also while typing all this above, I remembered that the API call is async (I'm using ASP.NET MVC 5 for the API) and maybe that has something to do with it? 同样,在上面键入所有这些内容时,我还记得API调用是异步的(我正在使用API​​的ASP.NET MVC 5),也许与此有关吗?

I remembered that the API call is async [...] and maybe that has something to do with it? 我记得API调用是异步的,也许与它有关?

You're correct. 没错 Unfortunately you can't just return a value from an async call, but you'll have to use a callback, or you could look into promises . 不幸的是,您不能仅从异步调用返回值,而是必须使用回调,或者可以查看promises Here's an example how you might refactor your code: 这是一个示例,您可以如何重构代码:

function getDataApiCall(query, onSuccess, onError) {
    $.ajax({
        url: "/Api/GetData/" + query,
            success: function (data) {
                console.log(data);
                onSuccess(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                onError(xhr.responseText);
            }
        });
}


function getData(query) {
    getDataApiCall(query, function(data) {
        if (data != null) {
            console.log(data);
            var template = $dataTemplate.html();
            var html = Mustache.to_html(template, data);
            $dataField.html(html);
        }
    }, function(err) {
        alert(err);
    }
}

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

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