Javascript requestData undefined despite var set for JSON

I am trying to get the weather via country and city using the openweather api, I have never coded using JSON before so please go easy on me

This is the code I am using:

<script type="text/javascript">
        $(document).ready(function () {
            $('#btnGetWeather').click(function () {
                var requestData = $('#txtCity').val() + ',' + $('#txtCountry').val();
                var resultElement = $('#resultDiv');

                $.ajax({
                    url : 'http://api.openweathermap.org/data/2.5/weather',
                    method : 'get',
                    data: { q:requsestData },
                    dataType: 'json',
                    success : function (data){
                        resultElement.html('Weather: ' + data.weather[0].main + '<br />' + 'Description: ' + data.weather[0].description);
                    }
                });
            });
        });
    </script>

The error I get on firebug is that q:requestData is undefined which confuses me, the html for the inputs is below here, I am sure that this is a simple error but I am unsure as to what this is

<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>

Answers


A few issues:

  1. There is a typo, you have declared var requestData and used it like requsestData. Remove a extra s between u and e.

  2. method should be POST, and not GET.


As others have pointed out, the cause of the issue is partly due to a typographical error. In the end your request should look something like this:

$.ajax({
  'method': 'POST',
  'url': 'http://api.openweathermap.org/data/2.5/weather',
  'data': {
    'q': city + ',' + country,
    'APPID': APIKEY,
  },
  'success': success,
});

The thing you will most likely need to add is your API key. Since October 2015 you'll need an API key to use Open Weather Map's API. Ref: http://openweathermap.org/faq#error401

You can get an API key here: http://openweathermap.org/appid

Then in the data object add the 'APPID' property and set its value to your API key.


Need Your Help

How to find a library with cmake?

linker cmake subdirectory

To link an executable with a library that resides in a standard location, one can do the following in a CmakeLists.txt file:

Mockup tools for developers: balsamiq sketchflow

php sketchflow mockups balsamiq

I got my first real project, but unfortunately it's with a client who doesn't quite know what he wants, which makes it a bit difficult to interact with him.