-1

I got JSON String object from backend.My Json String object contains id and en_name.Now i want to populate only en_name on my dropdown.

I ma getting this error:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data..

This is my JSON String Object.

[{"id":2773,"environments_name":"ABC-DEF"},{"id":2770,"environments_name":"ABCS- - DSDSDSFR"
        },{"id":2774,"environments_name":"GFDSS - GGGDFF"}] .



        //this my ajax call 
        <script>
            $(document).ready(function() {
                $("#customerDetails").change(function() {
                    var value1 = $('#customerDetails :selected').text();
                    $.ajax({
                        type : 'GET',
                        url : 'environments',
                        data : {
                            selectedcustomername : value1
                        },
                        success : function(result) {
                            getEnvNames(result);
                       }
                    });
                });
            });
        </script>

    //this is my function 
        <script language="JavaScript">
        function getEnvNames(result){
            alert("inside the function");
            var item= result;
            alert("rowwwww>>>>"+row);
            var select="<select id='environmentName'>";
            for(var i=0,i=row.length;i>1;i++){
                 var item=JSON.parse(row[i]);
                alert("for loop");
                 var item=row[i];

//Here I am getting item value is undefined.

                  alert("itemmmmmmmm>>>"+item);
                  alert("row[i],,,,,,>>>"+row[i]);
                  select+="<option value='"+item.id+"'>"+item.name+"</option>";
                } 
                select+="</select>";

                document.write(select);
        }
        </script>

        This is my JSP

        <td aligh="right">Select Environemnt : <select
                            name="environmentName" id="environmentName">
                            <option selected="selected" disabled="disabled">Select An Environment</option>
                            </select></td></tr>
mvswetha
  • 53
  • 8

1 Answers1

0

Change getEnvNames(result); to getEnvNames(JSON.parse(result));

Remove var item=JSON.parse(row[i]); from the processing loop

  • getEnvNames(JSON.parse(result));..i write like this,I am printing the value of result in my new method.I am getting relult is [object Object],[object Object],[object Object],[object Object]. – mvswetha Nov 22 '15 at 09:55
  • My JSp is not recogsing JSON Keyword. – mvswetha Nov 22 '15 at 09:57
  • That is correct because you are retrieving 4 objects. The browser will only show that they are objects and not the contents of the objects. When you loop through the objects - what happens? –  Nov 22 '15 at 10:01
  • item is undefined i am getting – mvswetha Nov 23 '15 at 05:22