Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
JavaScript
$(function () {

                 $.ajax({
                     type: 'POST',
                     datatype: 'text',
                     url: '../../Controllers/PopulateList.ashx',
                     data:JSON.stringify(DropDown),
                     success: function (jsonList) {
                       alert("success");
                       var listItems= "";

                     $.each(jsonList, function(index,itemData) {
                         alert(itemData.intId);
                         alert(itemData.strKey);
                            listItems+= "<option value='" + itemData.intId + "'>" + itemData.strKey + "</option>";
                        });
                          
//                        });
                            $("#SavAcc").html(listItems);
                     },
                     error: function (xhr, ajaxOptions, thrownError) {
                         alert('Error: ' + xhr.statusText);
                     },
                     processData: false, 
                     async: false,
                 });
             });
Posted
Updated 20-Sep-14 19:41pm
v2
Comments
NowYouSeeMe 25-Aug-14 7:18am    
so whats the problem?
OriginalGriff 25-Aug-14 7:24am    
And?
What have you tried?
Where are you stuck?

1 solution

I just wrote this this morning. It does the same thing but is written slightly different, but is proven to work rock solid for me. In this case I used a named / value pair to bring back productID' and Part Numbers.

I use to use something more complex like your example but simplified it over time.
I stopped using each to loop the array, I'm sure some will say that each is more efficient and faster.

I stopped using html to create the option element as well, and just use javascript to create the tag.

I url encode string value from the server, and unescape them for decoding.

$.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: "templateEditor.asmx/load_productSKUs",
   data: '{ "p_categoryID": -1}',
   dataType: "json",
   error: function (xhr, status, error) {
     var exitCode = 2;                                                  
     alert(xhr.responseText);                                                  
   },
   success: function (responseText) {
   var objB = jQuery.parseJSON(responseText.d);
   var exitCode = objB.exitCode;
   var productCount = objB.productCount;
   var productArray = objB.productArray;
   if (0 !== exitCode) {

   }
   else {
     $('#cke_105_select').empty();
     var option_s = document.createElement('option');
     option_s.innerHTML = '-- Select a Part Number --';
     option_s.value = '-1';
     $('#cke_105_select').append(option_s);
     var name, value, option;
     for (var pdx = 0; pdx < productArray.length; pdx++) {
       name = unescape(productArray[pdx].name.replace(/\+/g, " "));
       value = productArray[pdx].value;
       option = document.createElement('option');
       option.innerHTML = name;
       option.value = value;
       $('#cke_105_select').append(option);
     }
   }
 }
});


And the JSON
{
    "exitCode": 0,
    "productCount": 236,
    "productArray": [
        {
            "name": "03-12E",
            "value": 33
        },
        {
            "name": "03-12EX",
            "value": 270
        }
    ]
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900