標籤:arraylist com html code 資料 test org ict formdata
var formData = JSON.stringify(this.rows); //將表單中的資料轉為字串
$.ajax({ type: "post", url: ‘http://localhost:8080/data‘, data:formData, dataType:"json", contentType: "application/json;charset=utf-8", success: function (data) { }}); //開始漏掉了標紅的屬性設定,導致後台始終無法解析資料,能收到資料,但是很有亂七八糟的字元。
$.ajax http://www.runoob.com/jquery/ajax-ajax.html 有很多屬性可以設定
@RequestMapping(value = "/data",method = RequestMethod.POST) public void getData(@RequestBody String str) throws IOException{ org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper(); JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, TestModel.class); List<TestModel> list = (List<TestModel>)mapper.readValue(str, javaType); System.out.println(str); System.out.println(list); }
還有一個疑問未解決:
@RequestMapping(value = "/data",method = RequestMethod.POST) public void getData(@RequestBody LIst<TestModel> list)
無法接收到資料到list中
spring mvc $.ajax沒有指定contentType ,導致後台無法接收到資料