qq_22153431 2016-04-04 12:21 采纳率: 0%
浏览 1460

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应该没有问题啊,一直报错,希望大神能够解决一下,初次发帖没有悬赏,请各位大神见谅!谢谢

  • 写回答

2条回答 默认 最新

  • devmiao 2016-04-04 15:42
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境