使用Javascript、AJAX从JSP发布JSON数据
我想将JSON数据从JSP发布到SpringMVC控制器。为此,我使用Javascript-Ajax。请帮我做这个。我在网上搜索,但找不到问题的答案 当我执行代码时,我得到以下错误: 分析错误:不正确的语法错误输入意外结束使用Javascript、AJAX从JSP发布JSON数据,javascript,json,ajax,spring,Javascript,Json,Ajax,Spring,我想将JSON数据从JSP发布到SpringMVC控制器。为此,我使用Javascript-Ajax。请帮我做这个。我在网上搜索,但找不到问题的答案 当我执行代码时,我得到以下错误: 分析错误:不正确的语法错误输入意外结束,一些ID在控制台中打印如下:person@35fcfbc0, person@35fcfbc5` 但是我想在控制台中打印data1、data2和data3的值 我的Json字符串如下 {“persons”: [ {“data1”:”1000”, “data2”:
,一些ID在控制台中打印如下:
person@35fcfbc0, person@35fcfbc5`
但是我想在控制台中打印data1
、data2
和data3
的值我的Json字符串如下
{“persons”:
[
{“data1”:”1000”, “data2”:”false”,”data3”:”Holiday”},
{“data1”:”1000”, “data2”:”false”,”data3”:”Holiday”}
]
}
我使用下面的Javascript和Ajax发布数据
<script type="text/javascript">
$(document).ready(function()
{
var sJson
var ssJson
function dataRow(value1,value2,value3)
{
this.data1 = value1;
this.data2 = value2;
this.data3 = value3;
}
$('#help_button2').click(function()
{
// create array to hold your data
var dataArray = new Array();
var Vars
// iterate through rows of table
// * Start from '2' to skip the header row *
$(".case").each(function(i)
{
if (this.checked)
{
var Vars=i+1
//alert("Checkbox at index " + Vars + " is checked.");
var Vars1= $("#myTable tr:nth-child(" + Vars + ") td").eq(18).find('select').val()
//alert(Vars1)
//alert(Vars)
if (Vars1!="nooption")
{
dataArray.push
(
new dataRow
(
$("#myTable tr:nth-child(" + Vars + ") td").eq(1).html(),
$("#myTable tr:nth-child(" + Vars + ") td").eq(18).find('select').val(),
$("#myTable tr:nth-child(" + Vars + ") td").eq(19).find('select').val()
)
);
} //else {alert("Please Select Option for Error_NoError ")
//return false;
//}
}
});
ssJson1 = JSON.stringify({"persons" : dataArray });
alert(ssJson)
alert(ssJson1)
$.ajax({
url:'<%=webUrl%>/data/data',
type: 'POST',
dataType: 'json',
data:ssJson1,
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert(data.data1 + " " + data.data2 + " " + data.data3);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er+ssJson);
}
});
});
});
</script>
我的控制器代码:
@RequestMapping(value="/data",method=RequestMethod.POST)
public @ResponseBody void data(@RequestBody PersonList persons) throws
ParseException, IOException
{
List<Person> data1s=persons.getPersons();
System.out.println(data1s);
}
@RequestMapping(value=“/data”,method=RequestMethod.POST)
public@ResponseBody无效数据(@RequestBody PersonList persons)抛出
ParseException,IOException
{
List data1s=persons.getPersons();
系统输出打印项次(数据1s);
}
您确定发布的json对象有效吗?如果您更正json对象中的引号符号,是否会出现解析错误?您希望将数据打印到哪里?在Java端,还是在JavaScript端?是的,Json对象是有效的,即使我更正了引号,我也会得到解析错误。我想打印Java端的值,即spring控制器,然后将这些值插入SQL数据库
@RequestMapping(value="/data",method=RequestMethod.POST)
public @ResponseBody void data(@RequestBody PersonList persons) throws
ParseException, IOException
{
List<Person> data1s=persons.getPersons();
System.out.println(data1s);
}