在变量中存储getjson请求的问题

problems with storing getjson request in variable

本文关键字:请求 问题 getjson 存储 变量      更新时间:2023-09-26

我遇到了从getJSON()请求获得变量的麻烦。我有以下三个函数:

function getPcLatitude() { // onchange
    var funcid = "get_postcode_latitude";
    var postcode = parseInt($('#input-field-postcode').val());
    var jqxhr = $.getJSON('functions/getdata.php', {
        "funcid":funcid,
        "postcode":postcode}).done(function(dataLatitude) {
            if (dataLatitude == null) {
                //..
            } else {
                var myLatitude = 0;
                for (var i=0;i<dataLatitude.length;i++){
                   myLatitude = dataLatitude[i].pc_latitude;
                }
                return parseFloat(myLatitude);
                //alert(myLatitude);
            }
        });
}
function getPcLongitude() { // onchange
    var funcid = "get_postcode_longitude";
    var postcode = parseInt($('#input-field-postcode').val());
    var jqxhr = $.getJSON('functions/getdata.php', {
        "funcid":funcid,
        "postcode":postcode}).done(function(dataLongitude) {
            if (dataLongitude == null) {
                //..
            } else {
                var myLongitude = 0;
                for (var i=0;i<dataLongitude.length;i++){
                   myLongitude = dataLongitude[i].pc_longitude;
                }
                return parseFloat(myLongitude);
                //alert(myLongitude);
            }
        });
}
function getTop5Postcode() { // onchange
    setTimeout(function() {
        var funcid = "get_top_5_postcode";
        var er = rangeM3Slider.noUiSlider.get();
        var zv = $("#selectzv").val();
        if (zv < 1) {
            var zv = $("#selectzvfc").val();
        }
        var zp = $("#selectzp").val();
        if (zp < 1) {
            var zp = $("#selectzpfc").val();
        }
        var latitude = getPcLatitude();
        var longitude = getPcLongitude();
        var chosendistance = parseInt($('#input-field-afstand').val());
        var jqxhr = $.getJSON('functions/getdata.php', {
            "funcid":funcid,
            "er":er,
            "zp":zp,
            "zv":zv,
            "latitude":latitude,
            "longitude":longitude,
            "chosendistance":chosendistance}).done(function(dataPrices) {
                if (dataPrices == null) {
                    $('#myModalAlert').modal('show');
                } else {
                    //$('#myModalData').modal('show');
                    var table = '';
                    var iconClassZkn = '';
                    var iconClassIp = '';
                    for (var i=0;i<dataPrices.length;i++){
                       if (dataPrices[i].zkn_score == 0) {
                            iconClassZkn = 'no-score';
                       } else {
                            iconClassZkn = 'zkn-score';
                       }
                       if (dataPrices[i].ip_score == 0) {
                            iconClassIp = 'no-score';
                       } else {
                            iconClassIp = 'ip-score';
                       }
                       table += '<tr>'
                       + '<td width="75" class="zkh-image" align="center">'+ dataPrices[i].zvln_icon +'</td>'
                       + '<td width="250" align="left"><b>'+ dataPrices[i].zvln +'</b><br><i>Locatie: ' + dataPrices[i].zvln_city + '</i></td>'
                       + '<td class=text-center> € '+ dataPrices[i].tarif +'</td>'
                       + '<td class=text-center> € '+ dataPrices[i].risico +'</td>'
                       + '<td class=text-center><a target="_blank" href="' + dataPrices[i].zkn_url + '"><span class="' + iconClassZkn + '"><font size="2"><b>' + dataPrices[i].zkn_score + '</b></font></span></a></td>'
                       + '<td class=text-center><a target="_blank" href="' + dataPrices[i].ip_url + '"><span class="' + iconClassIp + '"><font size="2"><b>' + dataPrices[i].ip_score + '</b></font></span></a></td>'
                       + '</tr>';   
                    }
                    $('#top5').html(table);
                    //$('#myModalData').modal('hide');
                  }
            })
            .fail(function() { $('#myModalAlert').modal('show');}); //When getJSON request fails
    }, 0);
}

由于某种原因

var latitude = getPcLatitude();
var longitude = getPcLongitude();

部分不能工作/不能从函数中得到值。当我将两个函数的返回值转换为alert()时,它确实给了我期望值,因此这两个函数有效。

当我直接设置两个变量时,如下所示:

var latitude = 5215;
var longitude = 538;

getTop5Postcode()函数工作并填充表。

有什么帮助吗?

问候,巴特

不要忘记JavaScript是异步的,所以当您到达返回语句时,请求可能还没有完成。你可以使用承诺,比如:

 $.getJSON(....).then(function(value){//do what you want to do here})

你的函数(getpcl纬度和getPcLongitude)都不返回任何东西,因为返回语句是在异步请求的回调中,这就是为什么警报显示正确的值。

我建议你修改两个方法的签名,增加一个回调参数。

function getPcLatitude(callback) {
...
}
function getPcLongitude(callback) {
...
}

而不是返回你应该传递值给回调:

callback(parseFloat(myLatitude));
callback(parseFloat(myLongitude));

最后一个函数是这样的:

function getTop5Postcode() { // onchange
setTimeout(function() {
    var latitude;
    var longitude;
    getPcLatitude(function(lat) {
        latitude = lat;
        getTop5(); // Here you call the next function because you can't be sure what response will come first.
    });
    getPcLongitude(function(longi) {
        longitude = longi;
        getTop5();
    });

    function getTop5() {
        if (!latitude || !longitude) {
            return; // This function won't continue if some of the values are undefined, null, false, empty or 0. You may want to change that.
        }
        var funcid = "get_top_5_postcode";
        var er = rangeM3Slider.noUiSlider.get();
        var zv = $("#selectzv").val();
        if (zv < 1) {
            var zv = $("#selectzvfc").val();
        }
        var zp = $("#selectzp").val();
        if (zp < 1) {
            var zp = $("#selectzpfc").val();
        }
        var chosendistance = parseInt($('#input-field-afstand').val());
        var jqxhr = $.getJSON('functions/getdata.php', {
            "funcid":funcid,
            "er":er,
            "zp":zp,
            "zv":zv,
            "latitude":latitude,
            "longitude":longitude,
            "chosendistance":chosendistance}).done(function(dataPrices) {
                if (dataPrices == null) {
                    $('#myModalAlert').modal('show');
                } else {
                    //$('#myModalData').modal('show');
                    var table = '';
                    var iconClassZkn = '';
                    var iconClassIp = '';
                    for (var i=0;i<dataPrices.length;i++){
                    if (dataPrices[i].zkn_score == 0) {
                            iconClassZkn = 'no-score';
                    } else {
                            iconClassZkn = 'zkn-score';
                    }
                    if (dataPrices[i].ip_score == 0) {
                            iconClassIp = 'no-score';
                    } else {
                            iconClassIp = 'ip-score';
                    }
                    table += '<tr>'
                    + '<td width="75" class="zkh-image" align="center">'+ dataPrices[i].zvln_icon +'</td>'
                    + '<td width="250" align="left"><b>'+ dataPrices[i].zvln +'</b><br><i>Locatie: ' + dataPrices[i].zvln_city + '</i></td>'
                    + '<td class=text-center> € '+ dataPrices[i].tarif +'</td>'
                    + '<td class=text-center> € '+ dataPrices[i].risico +'</td>'
                    + '<td class=text-center><a target="_blank" href="' + dataPrices[i].zkn_url + '"><span class="' + iconClassZkn + '"><font size="2"><b>' + dataPrices[i].zkn_score + '</b></font></span></a></td>'
                    + '<td class=text-center><a target="_blank" href="' + dataPrices[i].ip_url + '"><span class="' + iconClassIp + '"><font size="2"><b>' + dataPrices[i].ip_score + '</b></font></span></a></td>'
                    + '</tr>';   
                    }
                    $('#top5').html(table);
                    //$('#myModalData').modal('hide');
                }
            })
            .fail(function() { $('#myModalAlert').modal('show');}); //When getJSON request fails
    }

}, 0);
}

当然,这离你的问题的完美解决方案还很远,但它应该可以工作!我没有测试这段代码

我通过在mysql查询中做一些额外的东西来解决这个问题。现在我只需要使用main函数。

事情现在工作了!谢谢你的帮助!