2012-07-19 58 views
1

我遇到了Spring MVC和Ajax的問題​​。我試圖給我的Spring控制器發送一個javascript列表,但我不能。我必須做一個搜索,我需要發送一個帶有一些參數的列表。用ajax發送列表到我的彈簧控制器

+0

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ – NimChimpsky 2012-07-19 08:26:56

+1

可能會有所幫助,如果你提供了一個簡短的例子你的javascript,ajax調用,你怎麼知道它不工作?你是否在服務器端或兩者都得到了javascript錯誤或錯誤? – km1 2012-07-19 03:12:17

回答

0

你將不得不轉換成列表,JSON,如果你通過AJAX發送,這From the spring blog itself

$("#account").submit(function() { 
    var account = $(this).serializeObject(); 
    $.postJSON("account", account, function(data) { 
     $("#assignedId").val(data.id); 
     showPopup(); 
    }); 
    return false; 
}); 


@RequestMapping(method=RequestMethod.POST) 
public @ResponseBody Map<String, ? extends Object> create(@RequestBody Account account, HttpServletResponse response) { 
    Set<ConstraintViolation<Account>> failures = validator.validate(account); 
    if (!failures.isEmpty()) { 
     response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 
     return validationMessages(failures); 
    } else { 
     accounts.put(account.assignId(), account); 
     return Collections.singletonMap("id", account.getId()); 
    } 
} 
0

你要轉換您的列表,以JSON字符串,用它作爲一個AJAX參數之前

0

answer中,可能會有助於

jQuery的AJAX在客戶端

$.ajax({ 
     type: "POST", 
     url: "submit", 
     data:JSON.stringify(detailsArr), 
     success: function(html){ 
      alert("Submitted"); 
      } 
     }); 

,並在服務器端

@RequestMapping(value = "/search", method=RequestMethod.POST) 
public String yourMethod(@RequestBody String body){ 
//convert body to array using JSONLib, FlexJSON or Gson 
} 
相關問題