Javascript 我不熟悉jQuery和JSON。我需要设置字符串格式的帮助,以便可以对其进行迭代

Javascript 我不熟悉jQuery和JSON。我需要设置字符串格式的帮助,以便可以对其进行迭代,javascript,json,Javascript,Json,这是控制器 @请求映射(“/getDropDownAjax”) 公共无效forA(HttpServletRequest-req、HttpServletResponse-resp){ System.out.println(“通过ajax”); 字符串aString=service.getA(); 试一试{ resp.getWriter()写入(aString); }捕获(IOE异常){ //TODO自动生成的捕捉块 e、 printStackTrace(); } }在ajax上使用jQuery

这是控制器


@请求映射(“/getDropDownAjax”)
公共无效forA(HttpServletRequest-req、HttpServletResponse-resp){
System.out.println(“通过ajax”);
字符串aString=service.getA();
试一试{
resp.getWriter()写入(aString);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
在ajax上使用
jQuery.parseJSON(json字符串)
函数

 function callAjax(){
    $.ajax({
        type:"GET",
        url:"getDropDownAjax.htm",

        success:function(data)
                {
                var data = jQuery.parseJSON(data);
                alert("success");
                console.log(data);  

                },

        error:function(){
                alert("failed");
        },
    });

    }

查看评论

这就解决了问题。谢谢大家

成功:函数(数据)
{
警惕(“成功”);
控制台日志(数据);
var s=数据切片(1,-1);
警报;
var dept=s.split(“,”);
用于(部门中的var l)
{
警报(部门[l]);
}

},
console.log(data)为您提供了什么?data[0]:pg,data[1]:ugit提供了带括号的[pg,ug]在这种情况下,您的数据以字符串形式出现……首先将其解析为json
jQuery.parseJSON(data)
每当我使用jQuery.parseJSON(data)时,它都会给出以下错误:未捕获的SyntaxError:Unexpected token pit给出以下错误:未捕获的SyntaxError:Unexpected token pin alert它逐个显示所有字符:[p g,u g]总共7个警报,共7个字符
function callAjax(){
$.ajax({
    type:"GET",
    dataType:"json", //will tell jquery to parse result as json
    url:"getDropDownAjax.htm",

    success:function(data)
            {
            alert("success");
            console.log(data);

            //iterate over each elem of the array response
            for (var i in data) {
               alert(data[i]);
            }   

            },

    error:function(){
            alert("failed");
    },
});

}