Ajax - Send a Request to Server


Prev Tutorial Next Tutorial

Send a Request to Server using Ajax

XMLHttpRequest Object, following methods are allow to interact with the server.

PropertyDescription
open(method, url, boolean)Specifies the type of method, URL and Boolean(if true than handle asynchronous or false than handle synchronous)
  • method: type of request, GET or POST
  • url: the location of the file o the server (with path)
  • boolean: true (asynchronous) / false (synchronous)
  • optionally: You wish to login and password may be added to arguments
send("string")String: use only POST method request.

GET or POST Method

  • GET is simpler and faster than POST, so mostly use a GET.
  • POST request use when sending a large amount of data to the server, update contain on the server also POST is secure and robust method than GET.
  • POST request use to send the data to send to the server.

GET Method

Syntax

	
xmlhttp.open("GET", url, true)  // xmlhttp is variable name
xmlhttp.send()

Example Send a Request to Server using Ajax

	
req.open("GET", "ajax_demo.txt", true); // req is variable name
req.send(null);

Post Method

Syntax

	
xmlhttp.open("POST", url, true)   // xmlhttp is variable name
xmlhttp.send(String)

Example Send a Request to Server using Ajax

	
req.open("POST", "ajax_demo.txt", true); // req is variable name
req.send("hitesh");

Prev Tutorial Next Tutorial

Advertisements





Download Projects