2 qq 22153431 qq_22153431 于 2016.04.04 20:21 提问

springmvc jquery ajax 出现parser error

js代码:点击添加按钮后,会添加select,然后调用jQuery ajax后就会报错

 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript">
var count = 1;
function addFoodlist(intnumber){
    $.ajax({  
        url: "${pageContext.request.contextPath}/springmvc/getfoodlist.mvc",    //后台webservice里的方法名称  
        type: "post",  
        dataType: "json",  
        contentType: "application/json; charset=utf-8",  
        traditional: true,  
        success: function (data) {  
            var id = "select1" + (count-1);
            for (var i in data) {  
                var jsonObj =data[i];  
                var optionstring = "";  
                for (var j = 0; j < jsonObj.length; j++) {  
                    optionstring += "<option value=\"" + jsonObj[j].name + "\" >" + jsonObj[j].type + "</option>";  
                }  
                $("#id").html("<option value='请选择'>请选择...</option> "+optionstring); 

            }  
        },  
        error: function (status, errorThrown) {  
            alert("状态码:" + status.status);
            alert("错误原因:" + errorThrown); 
        }  
    });            
}

var HTML2 = '<select id="select2' + count + '">'
+'</select>';
var HTML1 = '<select id="select1' + count + '" onchange="setSecond(this)">'
+'</select>';
    function add() {  
          var tbl = document.all.ci;  
          var rows = tbl.rows.length;  
          var tr = tbl.insertRow(rows);  
          var name = tr.insertCell(0);  
          name.innerHTML = HTML1;  
          var tel = tr.insertCell(1);  
          tel.innerHTML = HTML2;  
          var rdo = tr.insertCell(2);  
          rdo.innerHTML = '<input type="radio" value="0" name="rdo' + count + '">Yes <input type="radio" value="1" name="rdo' + count + '">No';  
          var chk = tr.insertCell(3);  
          chk.innerHTML = '<input type="checkbox" value="0" name="chk' + count + '">Modify <input type="checkbox" value="1" name="chk' + count + '">Delete';  
          var del = tr.insertCell(4);  
          del.innerHTML = '<input type="button" onclick="del(this)" value="Delete">';
          addFoodlist(count);
          count++;  
        }  

        function del(btn) {  
          var tr = btn.parentElement.parentElement;  
          var tbl = tr.parentElement;  
          tbl.deleteRow(tr.rowIndex-1);  
        }  
        function setSecond(obj){  
            var val = obj.value;
            var val2 = obj.id;
            var val1 = obj.id.substring(7,9);
            if(val == 'en'){  
                var sec = document.getElementById('select2' + val1 + '');  
               // document.write(val1);
                sec.innerHTML = uHTML;  
            }else{  
                var sec = document.getElementById('select2' + val1 + '');  
               // document.write(val1);
                sec.innerHTML = mHTML;  
            }     
        } 
</script>

controller代码:

 @RequestMapping(value="/getfoodlist",method=RequestMethod.POST)
    public ModelAndView getfoodlist(){
        ModelAndView mav = new ModelAndView("/index");
        List<FoodlistPo> list = null;
        list = FoodlistDao.queryAll();
        JSON jsonArray = ListToJson.listToJson(list);
        mav.addObject("data",jsonArray);
        return mav;
    }

listToJson

import java.util.List;

import net.sf.json.JSONArray;

public class ListToJson {
    public static JSONArray listToJson(List list){

        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray.toString());
        return jsonArray;
    }
}
打印出来的list的值:

[{"name":"叶类","type":"31"},{"name":"奶制品","type":"53"},{"name":"家畜","type":"51"},{"name":"家禽","type":"52"},{"name":"小吃","type":"71"},{"name":"干果","type":"42"},{"name":"干豆","type":"21"},{"name":"根茎","type":"33"},{"name":"水果","type":"41"}]

然后一直报parser error,但是这个json应该没有问题啊,一直报错,希望大神能够解决一下,初次发帖没有悬赏,请各位大神见谅!谢谢

3个回答

CSDNXIAON
CSDNXIAON   2016.04.04 20:32

jquery+ajax+springmvc
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

devmiao
devmiao   Ds   Rxr 2016.04.04 23:42
qq_22153431
qq_22153431 加了text还没没有用啊。。。
一年多之前 回复
qq_22153431
qq_22153431   2016.04.06 15:05

为什么没人来啊,大家快来啊

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!