0

I am trying to integrate open weather api in my application and I have invoked weather api and it's returned xml response. But when I am trying to get the values it's throwing error like unknown error.

My code is:

js code:

<script type="application/javascript">

        jQuery(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "http://api.openweathermap.org/data/2.5/forecast/daily?q=Hyderabad,IN&mode=xml&appid=c9d49310f8023ee2617a7634de23c2aa",
                dataType: "xml",
                success: function(data) {
                    /* handle data here */
                    console.log(data);

                    //$('<p>Text</p>').appendTo('#weather_report');

                    /*var wrapper = $("#weather_report");
                     wrapper.empty();
                     wrapper.append("<div class='city'> <p>Place: " + data.city.name + "</p><p>Country: " + data.city.country + "</p></div>");
                     wrapper.append("<div class='day_1'> <p>Place: " + data.city.name + "</p><p>Country: " + data.city.country + "</p></div>");*/

                },
                error: function(xhr, status) {
                    /* handle error here */
                }
            });
        });

    </script>

above code returning below format:

enter image description here

Could you please suggest me how to get the xml values to display?

1 Answer 1

2

You can use jQuery.parseXML

<script type="application/javascript">

jQuery(document).ready(function(){
    $.ajax({

        ...

        success: function(data) {

            ...

            var xml = jQuery( data );

            var nm = xml.find("weatherdata").find("location").find("name").text();

            console.log("City: " + nm);

            //For iterating through `forecast > time` tags, use

            jQuery(xml).find("time").each(function(){
                console.log("Day: " + jQuery(this).attr("day")  );
                console.log("Name: " + jQuery(this).find("symbol").attr("name")  );
            });


            ...

        },

        ...

    });
});

</script>

For more examples, check here : Read AND Process XML by jQuery

4
  • I have tried above solution and it's returning xmlDoc null values. Commented Jun 20, 2016 at 9:40
  • check updated answer Commented Jun 20, 2016 at 9:46
  • no luck still xmlDoc returning null Commented Jun 20, 2016 at 9:49
  • check updated answer and it's working now Commented Jun 20, 2016 at 10:45

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.