使用Javascript,AJAX从JSP发布JSON数据

时间:2016-12-23 12:42:05

标签: javascript json ajax spring

我想将JSON数据从JSP发布到Spring MVC Controller。为此,我使用的是Javascript Ajax。请帮帮我。我在网上搜索但无法解决我的问题。

当我执行代码时,我得到了以下错误:

  

分析错误:语法错误错误意外结束输入and some Ids get printed in a console as below: person @ 35fcfbc0,person @ 35fcfbc5`

但我希望在控制台中打印data1data2data3的值;
我的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>

我的Pojo课程

public class Person implements Serializable{
private int data1;
private String data2;
private String data3;
public int getData1() {
    return data1;
}
public void setData1(int data1) {
    this.data1 = data1;
}
public String getData2() {
    return data2;
}
public void setData2(String data2) {
    this.data2 = data2;
}
public String getData3() {
    return data3;
}
public void setData3(String data3) {
    this.data3 = data3;
}

我的控制器代码:

 @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);
 }

0 个答案:

没有答案