ajax获取只需要客户端更改比如用jQuery.getJSON(url, function(data){console.log(data)});

解决方案 »

  1.   

    楼主,如果是ajax请求,返回用json格式返回就ok。ajax的回调函数都可以读取后台返回的数据
      

  2.   


     $.ajax({
        url: url地址,
        data: {
    参数1: 值1,
    参数2: 值2
        },
        success: function(data) {
    var array = eval('(' + data + ')');//处理后的json
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert(textStatus);
    alert(errorThrown);
        }
    });
      

  3.   

    json
      

  4.   

    前台解析json太爽了,直接obj.key的形式
      

  5.   

    //要请求的一级机构JSON获取页面
             $.getJSON("loadDistrictAndStreetServletAndTypes?method=getAllDistrict",function (data) {
             //对请求返回的JSON格式进行分解加载
             $(data).each(function () {
                 $("#district").append($("<option/>").text(this.name).attr("value",this.id));
                });
             });
      

  6.   

    其实JS是通过XMLHttpRequest对象来支持Ajax,而且所有现代浏览器均支持 XMLHttpRequest 对象(当然IE5和IE6使用的是ActiveXObject);另外JSON就是一个字符串,当Servlet返回这个字符串后,剩下的就是前端Ajax怎么解析它了; 而XMLHttpRequest有个属性responseText,它就是用来 获得字符串形式的响应数据,所以剩下的就是:
    读取 JSON 字符串
    用 eval() 处理 JSON 字符串根据你的代码示例,你返回的应该是一个数组,可以这样解析:<script type="text/javascript">
    function loadJSON(){
        var xmlhttp;
        if (window.XMLHttpRequest){
             xmlhttp = new XMLHttpRequest();
         }else{
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }    xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                var x;
                var strText = "";
                var jsonResult = eval("(" + xmlhttp.responseText + ")");
             //var jsonResult = JSON.parse(xmlhttp.responseText);
             for(x in jsonResult){
                 if(x == 0)
             strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName;
                 else
                 strText = strText + "; " + jsonResult[x].firstName + ", " + jsonResult[x].lastName;
                }

               alert(strText);
             }
       }
       
       xmlhttp.open("post","写上实际的URL", true);
       xmlhttp.send();
    }</script>
    当然现代浏览器都提供了原生的 JSON 支持,你可以直接用JSON 解析器解析了,就是上面注释的代码;
      

  7.   

    注意:
    strText = jsonResult[x].firstName + ", " + jsonResult[x].lastName;
    这里要写成你实际对象的字段名字;