ajaxjsonvalidationjquery-mobilejsonlint

JSON returned AJAX post jQuery undefined


I am using jQuery to get a list of Names as JSON string -

`

$.ajax({ type: 'POST', url: "../Names.aspx", dataType: 'json', contentType: "application/json; charset=utf-8", data: "NAME=ALL_PROFILES", success: function (data, textStatus, jqXHR)` {

                var html = '';


                var obj = jQuery.parseJSON(data);
                $.each(obj, function (key, val) {
                    html += '<option value="' + val + '">' + val + '</option>';
                    alert("Success" + val);
                })
                $('#selName').append(html);
                $('#selName').selectmenu('refresh');


            },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('Error ' + jqXHR.val + errorThrown.val + textStatus.val);
                }
            });

The back end is an asp.net page, and when I debug I get the string as "{"Name":["Maria","John","Raj","Rosh","Tony","Name","test3","test4","test5","test6"]}", which I put in JSONLint and validated. It returns an undefined error in the AJAX request above. If I try with a single string "Name":"John", it works perfectly. The function for adding option is not correct for an array of JSON strings(I will work on it once it enter the success block), but I don't understand why it returns an undefined error when it is returning a valid JSON string. Any help would be appreciated.


Solution

  • Put an extra paranthesis around the result and it works.It is strange since JSONLint is not validating it now