I have a button, the event would be onclick and then the javascript function will send to server the request(for example a rating button on an item), the page should not be redirected, basically I want the event to happen in the background. How to do that? Please give me some idea or code.
JavaScript has great modules and methods to make HTTP requests that can be used to send or receive data from a server side resource.
To send an HTTP POST request, we need to first create the object by calling new XMLHttpRequest() and then use the open() and send() methods of XMLHttpRequest. To receive notifications when the status of a request has changed, we need to subscribe to the onreadystatechange event.
XMLHttpRequest is a built-in browser object that allows to make HTTP requests in JavaScript. Despite having the word “XML” in its name, it can operate on any data, not only in XML format.
HTTP GET: The Hypertext Transfer Protocol(HTTP) Get method is mainly used at the client (Browser) side to send a request to a specified server to get certain data or resources. Using this method the server should only let us receive the data and not change its state.
Welcome to the marvelous world of AJAX. One of the issues with ajax calls is that different browsers have different implementations of ajax. It's better if you can use a javascript library that abstracts these differences. I'd suggest you to pick up jQuery. You can make ajax calls in jQuery using $.get()
, $.post()
or $.ajax()
Check out jQuery.ajax.
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