1. Create POJO to map with JSON object
public class ActivitiKerjaForm { String[] idKodDuti; String[] flagKomponen; public String[] getIdKodDuti() { return idKodDuti; } public void setIdKodDuti(String[] idKodDuti) { this.idKodDuti = idKodDuti; } public String[] getFlagKomponen() { return flagKomponen; } public void setFlagKomponen(String[] flagKomponen) { this.flagKomponen = flagKomponen; } }2. Spring Controller
@ResponseBody @RequestMapping(method = RequestMethod.POST, value = "/secure/ppt/sbkPenilaianKendiri/saveAktivitiKerja", consumes = {"application/json"}) public String saveAktivitiKerja(@RequestBody ActivitiKerjaForm jsonString) { System.out.println("--- saveAktivitiKerja ---"); //System.out.println("IdKodDuti: " + jsonString.getIdKodDuti()); String[] idKodDuties = jsonString.getIdKodDuti(); for (String idKodDuti : idKodDuties) { System.out.println("idKodDuti: " + idKodDuti); } ObjectMapper mapper = new ObjectMapper(); String jsonInString = ""; try { jsonInString = mapper.writeValueAsString("success"); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonInString; }
3. JQuery Ajax
//Mix string and array format //var jsonString = {"idSbkNilaiKen":"123","idKodDuti":["1","2"],"flagKompeten":["Y","T"]} //Array format only var jsonString = { idKodDuti:["1","2"], flagKomponen:["Y","T"] } var urlName = 'sbkPenilaianKendiri/saveAktivitiKerja'; $.ajax({ url:urlName, type:"POST", dataType: 'json', contentType: "application/json", data: JSON.stringify(jsonString), async: false, //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation processData:false, //To avoid making query String instead of JSON success: function(text){ // Success Message Handler alert(text); console.log("my text: " + text); }, error: function (jqXHR,textStatus,errorThrown) { alert('error'); console.log('jqXHR:'); console.log(jqXHR); console.log('textStatus:'); console.log(textStatus); console.log('errorThrown:'); console.log(errorThrown); } });
No comments:
Post a Comment