在使用springmvc ajax json jquery 时,从后台传过来的数据报错:missing ] after element list parsing JSON。
原因是:
What is probably happening is that your $.post call is returning a JSON object already. jQuery will try to detect JSON automatically and parse it for you. When you call eval on a JSON object like this, you see this error. Sneaky! Just use the data object as is.
代码见:
function getCities(){
var city =document.getElementById("city").value;
alert(city);
$.ajax({
url:"${path}/cms/datadic/getCities.do",
type:"GET",
data:"id="+city,
contentType: 'application/json',
dataType:"json",
cache:false,
success:function(json){
// alert(json);
// var d = eval('('+ json +')'); 错误,因为后台传过来的就是json对象,不需要再转换(在控制台能看到错误提示)
// alert(d);
var html = '<option value="">--请选择--</option>';
var d = json.root;
for(var i in d){
var item = d[i];
html += '<option value="'+item.id+'">'+item.value+'</option>';
}
$("#icity").html(html);
}
});
}
后台传过来的数据如下:
{"root":[{"id":"226","value":"太原市"},{"id":"237","value":"大同市"},{"id":"249","value":"阳泉市"},{"id":"255","value":"长治市"},{"id":"269","value":"晋城市"},{"id":"276","value":"朔州市"},{"id":"283","value":"晋中市"},{"id":"295","value":"运城市"},{"id":"309","value":"忻州市"},{"id":"324","value":"临汾市"},{"id":"342","value":"吕梁市"}]}