0

私は次のようなjQueryajax呼び出しを持っています:

var arr = ["a", "b", "c"];
$.get("/test", {testArray: arr}, function(data) {
    alert(data);
});

サーバ側:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String addAnotherAppointment(
        HttpServletRequest request,
        HttpServletResponse response,
                    @RequestParam("arr") arr,
        Model model,
        BindingResult errors) {
}

では、どのようにパラメーターを受け取るのですか?

ありがとう。

4

2 に答える 2

3

Spring 3.0 ++を使用する場合は、「@ResponseBody」アノテーションを使用してください。

jsonリクエストを送信し、jsonからの応答を受信しますか?その場合は、次のようにコードを変更します。

var list = {testArray:["a", "b", "c"]};
$.ajax({
    url : '/test',
    data : $.toJSON(list),
    type : 'POST', //<== not 'GET',
    contentType : "application/json; charset=utf-8",
    dataType : 'json',
    error : function() {
        console.log("error");
    },
    success : function(arr) {
        console.log(arr.testArray);
        var testArray = arr.testArray;
         $.each(function(i,e) {
             document.writeln(e);
         });
    }
  });

サーバ側:

  1. 独自の「Arr」クラスを作成します。

    public class Arr {
     private List<String> testArray;
     public void setTestArray(List<String> testArray) {
         this.testArray = testArray;
     }
     public List<String> getTestArray() {
         return testArray;
     }
     }
    

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody// <== this annotation will bind Arr class and convert to json response.
       public Arr addAnotherAppointment(
       HttpServletRequest request,
        HttpServletResponse response,
        @RequestBody Arr arr, 
        Model model,
        BindingResult errors) {
            return arr;
    }
    
于 2013-01-29T03:04:21.547 に答える
1

@RequestParam("arr") arr,に変更@RequestParam("testArray") String[] arr

また、HTTPメソッドをからgetに変更しますpost

@RequestParamの値は、j​​queryajaxから送信されたパラメーター名と一致する必要があることに注意してください。あなたの場合、パラメータ名はtestArrayであり、値はですarr

于 2013-01-29T05:41:48.527 に答える