Creating json object in javascript and sending it to servlet

前端 未结 2 1614
失恋的感觉
失恋的感觉 2020-12-20 07:20

i am creating json object to save data and then sending it to the servlet. But when i try to retrieve the object and display its contents in java servlet, it throws me an er

相关标签:
2条回答
  • 2020-12-20 07:39

    On the Java side of things you need to convert the JSON array to a Java POJO bean first to be able to (easily) do something with it. There are multiple APIs to do this; Google GSON and Jackson are two possibilities.

    Jackson is used internally by Jersey, the reference implementation of the JAX-RS API; it works very well in my experience.

    0 讨论(0)
  • 2020-12-20 07:52

    What you build isn't a "JSON object", but a plain javascript object.

    You must encode your object in JSON :

    var param = '?objarray=' + JSON.stringify(arrayOfObjects);
    $.ajax({
         url: '/ProjectName/finalXmlServGen'+param,
         type: 'POST', 
         dataType: 'json',  
         success: function(result) {
           alert('SUCCESS');
         }
    });
    

    Or you may let jQuery do the encoding :

    $.ajax({
         url: '/ProjectName/finalXmlServGen',
         type: 'POST', 
         dataType: 'json',
         data: {objarray: arrayOfObjects}
         success: function(result) {
           alert('SUCCESS');
         }
    });
    
    0 讨论(0)
提交回复
热议问题