0

So I have some less than desireable JSON being returned from a web service and it goes a little somethin like this (please excuse the '?' characters):

{
    "d": [
      "AI,Appenzell Innerrhoden ", 
      "AR,Appenzell Ausserrhoden ", 
      "BE,Bern ", 
      "BL,Basel", 
      "BS,Basel", 
      "FR,Fribourg ", 
      "GE,Gen�ve ", 
      "GL,Glarus ", 
      "GR,Graub�nden ", 
      "JU,Jura ", 
      "LU,Luzern ", 
      "NE,Neuch�tel ", 
      "NW,Nidwalden ", 
      "OW,Obwalden ", 
      "SG,Sankt Gallen ", 
      "SH,Schaffhausen ", 
      "SO,Solothurn ", 
      "SZ,Schwyz ", 
      "TG,Thurgau ", 
      "TI,Ticino ", 
      "UR,Uri ", 
      "VD,Vaud ", 
      "VS,Valais ", 
      "ZG,Zug ", 
      "ZH,Z�rich "
    ]
}

I'm trying to populate a select list ('#state') but I keep getting 'undefined' Here's the ajax call - issue in success function:

var country = {};
country.request=$('#country').val();
$.ajax({
        type: 'POST',
        url: 'webserviceURL',
        crossDomain: true,
        data: JSON.stringify(country),
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        success: function(data){
          var toAppend = '';
           $.each(data.d,function(i,o){
            toAppend += '<option>'+o.id+'</option>';
          });
         $('#state').append(toAppend);
        },
        error: function (xhr, errorType, exception) {
            var errorMessage = exception || xhr.statusText;

            alert(errorMessage);
        }
    });
});

UPDATE: Reflecting first comment, now displays 'undefined' but has the correct number of 'undefined's'

UPDATE 2: I need the formatting to be<option value="AI">Appenzell Innerrhoden</option>

7
  • 2
    try, $.each(data.d,function(i,o) Commented Nov 5, 2014 at 1:16
  • @BatScream it is returning 'undefined' for each item, although it is shoing the correct number of 'undefined' Commented Nov 5, 2014 at 1:19
  • 1
    try, toAppend += '<option>'+o+'</option>'; Commented Nov 5, 2014 at 1:25
  • Man that's close. Probably my fault for not being clear enough. I need the 'AI' to be the option value and the 'Appenzell Innerrhoden' to be the option text. So it needs to be <option value="AI">Appenzell Innerhoden</option> Commented Nov 5, 2014 at 1:27
  • 2
    o.split(",")[0] will give you AI, and o.split(",")[1] will give you Appenzell Innerhoden. Commented Nov 5, 2014 at 1:29

2 Answers 2

1

You could modify your $each function parameter as below:

$.each(data.d,function(i,o){
var id = o.split(",")[0];
var value = o.split(",")[1];
toAppend += "<option value="+id+">"+value+"</option>";
});

The problem with your code was, you were accessing the JSON object rather than the d key inside it.

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

2 Comments

I like that, is there any benefit to doing each key in a var, as opposed to doing it in the toAppend function?
It depends upon the developers. Some follow it as a good programming practice. It becomes easy to read and maintain the code, that way.
1

Please change:

toAppend += '<option>'+o.id+'</option>';

To:

toAppend += '<option value="' + o.split(',')[0] + '">' + o.split(',')[1] + '</option>';

1 Comment

Yup thats exactly what @BatScream had in his comment. Works.

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.