开发者

How to get all request parameter names and values in a form submitted using ajax in spring MVC?

开发者 https://www.devze.com 2023-04-12 22:33 出处:网络
I am submitting a form in spring g MVC using ajax. The form is being generated dynamically. Hence I do not knowthe names of the request parameters submitted in the form.

I am submitting a form in spring g MVC using ajax. The form is being generated dynamically. Hence I do not know the names of the request parameters submitted in the form.

In a simple form submission (not Ajax), I used to get all the parameter names and their values using the following code.

Enumeration requestPa开发者_JAVA技巧rameters = request.getParameterNames();
while (requestParameters.hasMoreElements()) {
    String element = (String) requestParameters.nextElement();
    String value = request.getParameter(element);
}

But the same code does not work when I submit the form using ajax. Below is the code I am using to submit the form using ajax.

$.post("saveEntity", function() {
     alert("SUCCESS");
});

And below is my controller class method which gets called on the form submission.

@RequestMapping(method = RequestMethod.POST, value = "/saveEntity")
public @ResponseBody
void saveEntity( HttpServletRequest request) {

    Enumeration requestParameters = request.getParameterNames();

    while (requestParameters.hasMoreElements()) {
        String element = (String) requestParameters.nextElement();
        String value = request.getParameter(element);

        if (element != null && value != null) {
            logger.info("param Name : " + element
                    + " value: " + value);

        }
    }

}

Is there anything that I am doing wrong or missing something? Please help.


I will start by making sure whats being sent to the server, either look at request in firebug net panel or by using fiddler and make sure data is being sent properly.

XMLHttpRequest monitoring http://getfirebug.com/network

Edit

You are not sending any data along with your $.post.

$.post({
       url: "saveEntity", 
       data: $('form').serialize(),
       success: function(data) {
                    alert("SUCCESS");
                }
       });

Edit 2

Make sure $('form') is actually selecting the form which you want to post. If not, provide correct jquery selector by specifying form id. Once correct form is selected use alert($('form').serialize()); to make sure its returning data in this format {elementname1: elementvalue, elementname2: elementvalue2}. If not then make sure elements have name attribute assigned to them.


if you are using jquery then you can use

alert($('form').serialize());

to get the query string

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号