1

I am trying to display all of the continents but all I am getting is '[object Object]' instead of the actual continent names. How can I actually get the continent names to show?

AJAX

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

$.get('auto.php',{act:'fetchContinent'},loadContinents ,'json');

$("select#continents").change(function(){        
             $('div#display').html($("select#continents option:selected").text())});

function loadContinents(data){
          var optionStr = "";
          for (var index in data) {
              optionStr += '<option value="' + data[index] + '">' + data[index] + '</option>\n';
          }
          $("select#continents").html(optionStr);}                           
});

9
  • JSON.stringify(data) Commented Mar 8, 2015 at 22:39
  • @TommyTopas I don't understand how to use that Commented Mar 8, 2015 at 22:57
  • please, post your ajax call also Commented Mar 8, 2015 at 23:01
  • does it throw any error? Commented Mar 8, 2015 at 23:12
  • 1
    the returned array contains 0: {Continent: "Asia"} 1: {Continent: "Europe"} 2: {Continent: "North America"} 3: {Continent: "Africa"} 4: {Continent: "Oceania"} 5: {Continent: "South America"} 6: {Continent: "Antarctica"} Commented Mar 8, 2015 at 23:17

1 Answer 1

1

You get [object Object] because the array returned from the server contains objects! Since those are in the form {Continent: "thecontinent"}, you can write the option like this:

optionStr += '<option value="' + data[index].Continent + '">' + data[index].Continent + '</option>\n';

data[index] is the object {Continent: "thecontinent"}
data[index].Continent is the value of the Continent property ot that object

Sign up to request clarification or add additional context in comments.

Comments

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.