0

I am newbie with javascript and jquery and i am facing a problem in json parsing with jquery. I am using open weather simplest API for fetching the weather information but nothing is happening. If i hardcode the json in any file or in my script it works fine but as soon as i use URL of API it stop showing result.

here is my complete code:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    function myFunction() {
        var path = "api.openweathermap.org/data/2.5/weather?q=Lahore,pk";
        $(document).ready(function () {
            $("button").click(function () {
                $.getJSON(path, function (data) {
                    $.each(result, function (i, field) {
                        $("div").append(field + " ");
                    });
                });
            });
        });
    }
</script>
</head>
<body>

<button onclick="myFunction()">Get JSON data</button>

<div></div>

</body>
</html>
6
  • 3
    You are looping through result. Where is it's definition ? Commented Apr 17, 2015 at 7:12
  • 4
    Where did you get the variable result? Did you mean it as data? Commented Apr 17, 2015 at 7:12
  • 2
    Missing http:// at the start of path. But does that website support CORS or JSONP? You should check your console for errors. Very important when debugging Javascript Commented Apr 17, 2015 at 7:12
  • 2
    Move the ready handler from the inside of myFunction. Commented Apr 17, 2015 at 7:16
  • @A1rPun . no i told that for Json format output print method.. example: alert(JSON.stringify(data)); data is his response Commented Apr 17, 2015 at 7:17

2 Answers 2

1

You did multiple mistakes. Here a working fiddle:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">    </script>
</head>
<body>

<button id="myButton">Get JSON data</button>

<div id="mydiv"></div>

</body>
<script>
$("#myButton").click(function(){
    var path = "http://api.openweathermap.org/data/2.5/weather?q=Lahore,pk";
    $.getJSON(path, function (data) {
        $.each(data, function (i, field) {
            var textNode = document.createTextNode(i+ " " +JSON.stringify(field));
            $("#mydiv").append(textNode);
        });
    });
});
</script>

Example

Write a comment if you need more information.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes its working! Thanks a lot for the help and yes i know i have done multiple mistakes as i am newbie.
Seeing you are new. If my answer was helpful for you, please consider marking it as accepted.
It is fetching the complete response perfectly but how can i get specific data like from the result if i want to know the temperature only how can i get that? Right now i am using this approach using your code: '$.getJSON(path, function (data) { $.each(data, function (i, field) { var textNode = document.createTextNode(i+ " " +JSON.stringify(field)); $("#mydiv").append(textNode); obj = JSON.parse(textNode); document.getElementById("mydiv").innerHTML = obj.main.temp;'
If you want to access fields in your response object do it like this: $.getJSON(path, function (data) { document.getElementById("mydiv").innerHTML = data.main.temp; });
Perfect!! Thanks a lot! Now let me see how can i vote and mark your answer as answered :)
1

Loop through the JSON result, ie. data in your case, :

Working Code:

<button>Get JSON data</button>
<div></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
    var path = "http://api.openweathermap.org/data/2.5/weather?q=Lahore,pk";
        $(document).ready(function () {
            $("button").click(function () {
                $.getJSON(path, function (data) {
                    $.each(data, function (i, field) {
                        $("div").append(JSON.stringify(field)); // parse the object according to your need.
                    });
                });
            });
        });
</script>

1 Comment

The openweathermap doesn't seem to support HTTPS, so your relative protocol would break here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.