Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

Getting BAD request error in REST API when passing latitude and longitude value as a variable

I am new to this platform and also in coding.I am trying to get AQI using REST API from a server. It works fine when I am giving the latitude and longitude value directly to the API. But it is giving bad request 400 error when I am passing latitude and longitude value as a variable. Please help me. My code snippet are as bellow

var aqival;
var level;
var sval;
var arrf;

var latitude =parseFloat(22.569719);
var longitude =parseFloat(88.36972);


var request = new XMLHttpRequest();
request.open("GET", "https://api.weatherbit.io/v2.0/current/airquality?lat=latitude&lon=longitude&key=API KEY", true);


console.log(latitude);
console.log(typeof(latitude));

request.onload = function () {

    var jsonobj = JSON.parse(this.response);
    if (request.status >= 200 && request.status < 400) {

        aqival = jsonobj.data[0].aqi;
        level = "poor";

    } else {
            console.log('error');
            }
    var arr = "kolkata,wb";
    var arrpad = arr.padEnd(23,' ');        
    sval = String(aqival);
    var valpad = sval.padEnd(4,' ');
    var levelpad = level.padEnd(6,' ');
    arrf = arrpad + valpad + leve`enter code here`lpad;
    console.log(arrf);
    
  // Begin accessing JSON data here
}

request.send(); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...