Is there any method in jquery like $.post , $.ajax etc which works like a normal form submission. I know about .submit()
but it requires a form element , can we send a normal request without a form element in jquery?
Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method.
GET vs POST in AJAX callsUnless you are sending sensitive data to the server or calling scripts which are processing data on the server it is more common to use GET for AJAX calls. This is because when using XMLHttpRequest browsers implement POST as a two-step process (sending the headers first and then the data).
ajax() method allows you to send asynchronous http requests to submit or retrieve data from the server without reloading the whole page. $. ajax() can be used to send http GET, POST, PUT, DELETE etc. request.
ajax returns a promise object, so that we don't have to pass a callback function.
window.location.href = '/controller/action?foo=bar';
You can submit without a form using $.ajax()
. Further, to make it behave like a normal form would, set the async
property to false
.
$.ajax({
url: "/controller/action",
data: {'foo':'bar'},
async: false
});
This will result in you being sent to:
"/controller/action?foo=bar"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With