Calling live json web service using jquery ajax

Here is the HTML and jQuery code used in the demo 
 
<html>
<head>
    <script src="jquery-1.11.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnGetWeather').click(function () {
                var resultElement = $('#resultDiv');
 
                var requestData = $('#txtCity').val() + ',' + $('#txtCountry').val();
                $.ajax({
                    url: 'http://api.openweathermap.org/data/2.5/weather',
                    method: 'get',
                    data: { q: requestData },
                    dataType: 'json',
                    success: function (response) {
                        if (response.message != null) {
                            resultElement.html(response.message);
                        }
                        else {
                            resultElement.html('Weather: ' + response.weather[0].main + '<br/>'
                                + 'Description: ' + response.weather[0].description);
                        }
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            });
        });
    </script>
</head>
<body style="font-family:Arial">
    <table>
        <tr>
            <td>City</td>
            <td><input type="text" id="txtCity" /></td>
        </tr>
        <tr>
            <td>Country</td>
            <td><input type="text" id="txtCountry" /></td>
        </tr>
    </table>
    <input type="button" id="btnGetWeather" value="Get Weather Data">
    <br /><br />
    <div id="resultDiv">
    </div>
</body>
</html>

Comments

Popular posts from this blog

Drag and Drop multiple File upload using jQuery AJAX in ASP.Net using C# and VB.Net

jQuery dynamic menu from database in asp.net

Automatically send Birthday email using C#.Net