Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send a http request in the background in javascript?

Tags:

javascript

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.

like image 233
Rn2dy Avatar asked Mar 07 '11 07:03

Rn2dy


People also ask

Can JavaScript send HTTP request?

JavaScript has great modules and methods to make HTTP requests that can be used to send or receive data from a server side resource.

How do you send a request in JavaScript?

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.

Which JavaScript object allows JavaScript to make a request to the web server?

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.

What is get HTTP?

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.


2 Answers

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()

like image 87
Munim Avatar answered Oct 29 '22 14:10

Munim


Check out jQuery.ajax.

like image 20
grc Avatar answered Oct 29 '22 15:10

grc