Ajax调用在函数内执行时会中断,并抛出警告。

huangapple go评论95阅读模式
英文:

Ajax Call Breaks When executed inside a function and throws a warning

问题

以下是您要翻译的 JavaScript 代码的翻译部分:

我试图DRY化一个JavaScript文件该文件进行了多个ajax调用我有一个函数用于发出ajax调用返回包含其他ajax调用所需的详细信息的对象

这个工作得很好为了使代码更加干燥我使用了makeCall函数进行了awaited的ajax调用问题是第二次对retrieveCountry.php的makeCall不会执行因为gejson特征从未添加到地图上我还收到了一个警告:“await对此表达式的类型没有影响ts(80007)

请问可能出了什么问题值得注意的是整个代码都在一个async函数内部detail对象的范围仅限于此async函数内部

希望这有助于解决您的问题。如果您需要更多帮助,请随时告诉我。

英文:

I am trying to DRY a JavaScript file that makes several ajax calls. I have a function that makes a ajax call, returns an object containing details needed for other ajax calls.

const makeCall = (url, type, data, done) => {
    $.ajax({
            url: url,
            type: type,
            dataType: "json",
            data: data,
        })
        .done(done)
        .fail(function(error) {
            console.log(error);
        });
};

const country = await $.ajax({
        url: "libs/php/opencage.php",
        type: "GET",
        dataType: "json",

        data: {
            LAT: x.coords.latitude,
            LNG: x.coords.longitude,
        },
    })
    .done(function(result) {
        if (result.status.name == "ok") {
            //populate the detail object with responses from opencage needed to communicate with other apis
            detail.lat = x.coords.latitude;
            detail.long = x.coords.longitude;
            detail.continent = result.data.results[0].components.continent;
            detail.countryName = result.data.results[0].components.country;
            detail.countryCode = result.data.results[0].components.country_code;

            let code = result.data.results[0].components.country_code;
            //set the select to user country
            console.log(code);
            $("#countrySelect").val(code.toUpperCase());
        }
    })
    .fail(function(error) {
        // your error code

        console.log(error);
    });

//save needed part of detail object for easy access
let latitude = detail.lat;
let longitude = detail.long;
let countryName = noSpace(detail.countryName);
let countryCode = detail.countryCode;
let continent = detail.continent;

//makecall to retrieve geojson feature details for selected country.
makeCall(
    "libs/php/retrieveCountry.php",
    "GET", {
        code: countryCode.toUpperCase()
    },
    function(r) {
        //add the geojson object to the map
        L.geoJSON(r).addTo(map);
    }
);

This works well. To DRY the code, I made the awaited ajax call with the makeCall function. what happens is that the second makeCall to retrieveCountry.php doesnt execute as the gejson feature is never added to the map. I also get a warning: 'await' has no effect on the type of this expression.ts(80007)

const country = await makeCall(
    "libs/php/opencage.php",
    "GET", {
        LAT: x.coords.latitude,
        LNG: x.coords.longitude,
    },
    function(result) {
        if (result.status.name == "ok") {
            //populate the detail object with responses from opencage needed to communicate with other apis
            detail.lat = x.coords.latitude;
            detail.long = x.coords.longitude;
            detail.continent = result.data.results[0].components.continent;
            detail.countryName = result.data.results[0].components.country;
            detail.countryCode = result.data.results[0].components.country_code;

            let code = result.data.results[0].components.country_code;
            //set the select to user country
            console.log(code);
            $("#countrySelect").val(code.toUpperCase());
            return detail;
        }
    }
);

//save needed part of detail object for easy access
let latitude = detail.lat;
let longitude = detail.long;
let countryName = noSpace(detail.countryName);
let countryCode = detail.countryCode;
let continent = detail.continent;

//makecall to retrieve geojson feature details for selected country. This part is never implemented as this feature is not added to the map.
makeCall(
    "libs/php/retrieveCountry.php",
    "GET", {
        code: countryCode.toUpperCase()
    },
    function(r) {
        //add the geojson object to the map
        L.geoJSON(r).addTo(map);
    }
);

Please what could be wrong? Also worth adding is that the entire code is inside an async function and the detail object is locally scoped to this async function

答案1

得分: 0

没有等待AJAX操作,警告是为什么的线索。具体来说,它告诉您在这里await无效:

await makeCall(...)

这是因为makeCall不可等待。它不是async函数,也不返回任何内容:

const makeCall = (url, type, data, done) => {
  $.ajax(...);
};

由于$.ajax()操作是可等待的,看起来您的意图是返回它:

const makeCall = (url, type, data, done) => {
  return $.ajax(...);
};

这将返回$.ajax()本身的可等待("then-able")结果,消费代码可以使用await(或跟随.then())来处理它。

英文:

Nothing is awaiting the AJAX operation, and the warning is the clue as to why. Specifically it's telling you that await does nothing here:

await makeCall(...)

This is because makeCall is not awaitable. It's not async and it doesn't return anything:

const makeCall = (url, type, data, done) => {
$.ajax(...);
};

Since the $.ajax() operation is awaitable, it appears that your intent was to return that:

const makeCall = (url, type, data, done) => {
return $.ajax(...);
};

This will return the awaitable ("then-able") result from $.ajax() itself, which consuming code could then await (or follow with .then()).

huangapple
  • 本文由 发表于 2023年7月31日 23:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定