How to post a form with many fields with jQuery?

后端 未结 7 2369
不知归路
不知归路 2020-12-15 06:46

Though $.ajax() can be used to do ajax things, I don\'t think its fit for posting values of a big form.

How would you post a big form (many fields) with

7条回答
  •  有刺的猬
    2020-12-15 07:18

    What is your reasoning behind that assumption? POST is designed for transferring larger amounts of data than GET. An AJAX POST request is almost exactly the same as a "normal" POST request, it's just bundled and handled internally by a browser in a slightly different manner. A couple of headers might be slightly different, but the data is all the same. Why should AJAX fail to handle a "large" form?

    What would you even define as a "large" form anyway?

    Edit: Thanks for the clarification on your question. I understand what you're asking now, and I see where you're coming from. For a form with a lot of inputs, it could be a pain to bundle it up into an Ajax request all the time.

    Since you're using jQuery, there's an easy solution to this. Check out the serialize() method. You give it a form, and it gives you back a query string of all the form input elements and values which you can pass directly to an ajax request. There's an example there on the manual page that shows how it's done.

    All you have to do is this:

    $.ajax({
        data: $("form").serialize(),
        //etc.
    });
    

    where "form" is the id of your form.

提交回复
热议问题