0

I have be writing code for Json requests, but haven't seen this before..
How do I go about it?

I want to return all the countries with their name and Phonecode

Here is the Json am getting request from:

data.json:

{
    "af": {
        "name": "Afghanistan",
        "phoneCode": "93"
    },
    "al": {
        "name": "Albania",
        "phoneCode": "355"
    },
    "dz": {
        "name": "Algeria",
        "phoneCode": "213"
    },
    "ad": {
        "name": "Andorra",
        "phoneCode": "376"
    }
 }

I want it to return this:
Html:

<div id="results"></div>

Javascript:

<script>
        $(document).ready(function() {

                //start ajax request
                $.ajax({
                    url: "data.json",
                    success: function(data) {
                        var json = $.parseJSON(data);
                        $('#results').html('Country: ' + json.[0].name);
                    }
                });
            });
        });

</script>

Thanks!

2
Contributors
3
Replies
13
Views
1 Year
Discussion Span
Last Post by diafol
1
function getData() {
        $.ajax({
            url: "countries.json",
            success: function (data) {
                var json = $.parseJSON(data);
                var output = '<ul>';
                $.each(json, function(i,v)
                {
                    output += '<li>ObjectName: ' + i + ' Country Name: ' + v.name + ' Number: ' + v.phoneCode + '</li>';
                });
                output += '</ul>';
                $('#results').html(output);
            }
        });
    }

Edited by diafol

0

Thanks for the reply @diafol, missed that '$.each()' function.
Please, If I do want to use it a typeahead like:

Typeahead one: Input country
Typeahead two: Input states

Html:

Country:
<input type="text" id="list-countries"/>

States:
<input type="text" id="list-states"/>

Pseudocode:

If 'typeahead one' = Canada
                                then 

                                'typeahead two' should list states in Canada: like texas...

Expecting your opinion soon.

0

This is a different question. Start a new thread. Mark this one solved if the original question was answered. Thanks.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.