Invalid place name giving in weather data project in input it still give some output

sir in project 2:showing weather data using html page when i give invalid place name it still give some output so how we can say the output it gives a correct output or the output of same place using API of weatherbit!
temp at ffffffffffff|494x287

Hi, our team will help you with this. Let me forward this query :writing_hand:

Hi,

Kindly share the project files with us so that we can look into it.

<head>

    <title>function-calling</title>

    <script type="text/javascript">

     

    </script>

</head>

<body>

    <p id="output">Tempreture</p>

    <input id="city">

    <button onclick="update_user()">submit</button>

    <script type="text/javascript">

        function update_user()

        {

       city=document.getElementById("city").value;

       httpRequest=new XMLHttpRequest();

       url="https://api.weatherbit.io/v2.0/current?city="+city+"&key=71a1115ce4b34df7951e1a34789a5295";

        httpRequest.open("GET", url);

        httpRequest.send();

        httpRequest.onreadystatechange=function()

        {

            if (this.readyState==4 && this.status==200)

            {

                user_data=JSON.parse(this.responseText);

                document.getElementById("output").innerHTML=user_data.data[0].temp;

            }

        }

     

         document.getElementById("output").style.color="red";

         document.getElementById("output").style.fontSize="30px";

        }

       

    </script>

</body>

sir all the code are same as instructed only the API creation is done with my own id it giving the tempreture but when i enter the invalid place name it still give any tempreture

Sir you not respond to my query

Sir you not respond to my query I have raise query before

Hi,

Apologies for replying late to your query.

Instead of matching complete city string, this API matches the city’s string patterns and whichever string it closely resembles it gives the output for that particular city.

There are two things you can do:

  1. Display the name of the city for which the temperature is fetched. Kindly refer to the below code and change only this part in your code.
 if (this.readyState==4 && this.status==200)

            {

                user_data=JSON.parse(this.responseText);
                console.log(user_data)

                document.getElementById("output").innerHTML= "Temperature for " + user_data.data[0].city_name + ", " + user_data.data[0].country_code + " is = " + user_data.data[0].temp;

            }
            else {
                document.getElementById("output").innerHTML="Could not find city- " + city;
            }
  1. After the API request sends the data. You can check the input’s city and received city if they are equal or not. If they are equal then simply display the temperature. Please find the code below.
if (this.readyState==4 && this.status==200)

            {

                user_data=JSON.parse(this.responseText);
                console.log(user_data)

                if (user_data.data[0].city_name.toLowerCase() == city.toLowerCase()) {
                    document.getElementById("output").innerHTML= "Temperature for " + user_data.data[0].city_name + ", " + user_data.data[0].country_code + " is = " + user_data.data[0].temp;
                }
                else {
                    document.getElementById("output").innerHTML="Could not find city- " + city;
                }


            }

Please reach out to me if you have any other issue or need some other help.

Thank you sir i got it

1 Like