Form submit submission form, submit a form difference form with ajax asynchronous,

Because the development of a small detail, originally uploaded the file, fill in the file name ajax submit, but did not get to the absolute address of the file,

So using a form submission form, that form and then submit the form + ajax link is better, do not change too much code.

 

the difference:

Form submission form: usually for page jump;

Ajax asynchronous submit: page can not jump;

 

Form form submission

<body>

    <div id="forms>

        <form id="form1" action="/users/login" method="post">

            <p>用户名:<input name="userName" type="text" id="userName" value="" /></p>

            <p>密 码:<input name="password" type="password" id="password" value="" /></p>

            <p><input type="submit" value="确认">&nbsp<input type="reset" value="重置"></p>

        </form>

    </div>

</body>

Form with the form action and method to determine the address and submitted to the submission;

Click the OK button will trigger a form submit form event, transfer data to the back end, page jump and then the data transfer is controlled by the servlet background.

 

Ajax implementation form submission:

<body>

    <div id="forms">

        <form id="form1">

            <p>用户名:<input name="userName" type="text" id="userName" value="" /></p>

            <p>密 码:<input name="password" type="password" id="password" value="" /></p>

            <p><input type="button" id="btn" value="登录">&nbsp<input type="reset" value="重置"></p>

        </form>

    </div>

    <script type="text/javascript">

     $("#btn").click(function () {

    $.ajax({ 

            type: method "POST", // submitted

            url: "/ user / login", // address submitted 

            data: $ ( "# form1") serialize (), // output value sequence of the form.

            async: false

            error:  function (Request) {// if it fails

                 alert ( "Commit failed error"); 

            }, 

            Success:  function (Data) {// Success

                 alert (data); // return data will be displayed

                 window.location.href = "jump page" 

            } 

         });

       }); 

    </script>

</body>

 

to sum up:

In the usual way, click on the login button type to "submit" type, form form of action is not empty;

In the embodiment to be noted ajax $ .ajax method parameters: provided dataTyp and data properties. But also to add an id attribute;

Guess you like

Origin www.cnblogs.com/Koaler/p/12047303.html