通过AJAX将数组POST到Spring控制器

时间:2011-10-06 08:35:57

标签: java jquery ajax

我试图通过jQuery AJAX将对象的数组发送到Spring控制器。 这是javascript代码:

var data = new Array();
            $.each(products, function (i) {
                var temp = {};
                temp.orpid = products[i].orpid;
                temp.orpah = $('#orpah' + products[i].orpid).is(':checked');
                temp.orpad = $('#orpad' + products[i].orpid).val();
                data.push(temp);
            });
            $.postJSON(url + 'save',
                    data,
                    function(response) {
                        if (response.isAuthenticated && response.isAuthorized) {
                            if (response.hasErrors) {
                                $('#routeForm').setErrors(response.errors);
                                hideWait();
                            }
                        }
                        else
                            redirectToLogin();
                    });

以下是发送到服务器的参数:

[{"orpid":10,"orpah":false,"orpad":""},{"orpid":11,"orpah":false,"orpad":""}]

控制器的方法有以下定义:

@RequestMapping(value = "/save", method = RequestMethod.POST, headers = BaseController.AJAX_HEADER)
public ModelAndView save(@RequestBody HandlingOrderProductActionForm form, HttpServletResponse response) {...}

其中HandlingOrderProductActionForm是:

public class HandlingOrderProductActionForm extends BaseForm {  
public class ActionForm {
    private Boolean orpah;
    private String orpad;
    private Long orpid;

    public ActionForm() { }

    public void setOrpah(Boolean orpah) {
        this.orpah = orpah;
    }

    public Boolean getOrpah() {
        return orpah;
    }

    public void setOrpad(String orpad) {
        this.orpad = orpad;
    }

    public String getOrpad() {
        return orpad;
    }

    public void setOrpid(Long orpid) {
        this.orpid = orpid;
    }

    public Long getOrpid() {
        return orpid;
    }
}

private List<ActionForm> actions;

/**
 * 
 */
public HandlingOrderProductActionForm() {
    super();
}

public void setActions(List<ActionForm> actions) {
    this.actions = actions;
}

public List<ActionForm> getActions() {
    return actions;
} }

问题是,服务器返回415错误:

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

有什么想法吗? 谢谢

1 个答案:

答案 0 :(得分:1)

问题解决了 - 只需将ActionForm移动到单独的类!