문제

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.

도움이 되었습니까?

해결책 2

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

다른 팁

If your aspx page returns valid JSON and you are using dataType: 'json', you should not use jQuery.parseJSON() on the returned data, since jQuery already parses the returned JSON string into an object.

If you use "Name":"John" it is not valid JSON, so JQuery enters the error callback.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top