jQuery ajax()

jQuery ajax() method allows you to perform an AJAX (asynchronous HTTP) request.It was existing since version 1.0.

Syntax

$.ajax(URL [, settings ])

Specification

  • URL:specifies url you wish to load.
  • settings:All settings are optional.
    • accepts:Content type sent in the request header that tells the server what kind of response it will accept in return.
    • async:By default, all sent request are asynchronous.
    • data:Data to be sent to the server when performing the Ajax request.
    • error:A function to be called if the Ajax request fails.
    • success:A function to be called if the Ajax request succeeds.
    • password:specifies a password to be used in an HTTP access authentication request.

Examples

change div data by jQuery ajax().

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<title>jQuery ajax() Example </title>
</head>
<body>
<div id="txt"><h2>Ajax Content</h2></div>
<button>Change Content</button>
</body>
</html>
Javascript
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: "POST",
url: "ajax.php",
data:{name:"Manish",location:"USA"},
success: function(msg)
   {
$("#txt").html(msg);    
   },
error: function(jqXHR, textStatus){
$("#txt").html("Request failed: " + textStatus);            
      }
            });
       });
});