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!

Recommended Answers

All 3 Replies

Member Avatar for diafol
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);
            }
        });
    }

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.

Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.