Difference between jquery post and html form post

后端 未结 1 1497
闹比i
闹比i 2021-01-07 08:26

im searching for a while but i dont get it:

What is the different between a Jquery $.post and when i submit a normal html form with post?

My jquery code:

相关标签:
1条回答
  • 2021-01-07 08:50

    What is the different between a Jquery $.post and when i submit a normal html form with post?

    For the most part, nothing. Both issue an HTTP POST request with the values from the form. One just does so in the context of the page, the other does so in an AJAX context without refreshing the page.

    The sever will just send the redirect as data and will not redirect the page.

    Therein lies a bit of a misunderstanding on your part. The server never redirects the browser. All the server ever does is send a header back to the browser suggesting that the browser perform a redirect.

    When the browser receives that header in the context of a page loading request, it performs the redirect. When the JavaScript code receives that header in the context of an AJAX request, it ignores it. AJAX responses, by design, don't reload the page.

    If you want the form post to result in a server-initiated redirect, use a normal form POST. You can still initiate this form POST from JavaScript, just not using $.post() since that's an AJAX request. Instead, you might do something like $('form').submit() or $('form input[type="submit"]').click(). It may take some tinkering.

    If, on the other hand, you want to use AJAX then any conditional redirect logic in response to the completion of the AJAX request would need to be in the client-side logic.

    0 讨论(0)
提交回复
热议问题