ajax() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:ajax

Description

The ajax() method performs an asynchronous HTTP (AJAX) request.

All jQuery AJAX methods use the ajax() method.

Syntax

$.ajax({name:value, name:value, ... })

The parameters specifies one or more name/value pairs for the AJAX request.

Possible names/values in the table below:

Name Value Description
async Boolean value whether the request should be handled asynchronous. Default is true
beforeSend(xhr)functionrun before the request is sent
cache Boolean value whether the browser should cache the requested pages. Default is true
complete(xhr,status) functionrun when the request is finished after success and error functions
contentTypestring The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
contextcontext "this" value for all AJAX related callback functions
data data data to be sent to the server
dataFilter(data,type) functionhandle the raw response data of the XMLHttpRequest
dataType data type data type expected of the server response.
error(xhr,status,error)function run if the request fails.
global Boolean value whether to trigger global AJAX event handles for the request. Default is true
ifModified Boolean valuewhether a request is only successful if the response has changed since the last request. Default is: false.
jsonp string A string overriding the callback function in a jsonp request
jsonpCallback string a name for the callback function in a jsonp request
password string a password to be used in an HTTP access authentication request.
processDataBoolean value whether data sent with the request should be transformed into a query string. Default is true
scriptCharset string the charset for the request
success(result,status,xhr) function A function to be run when the request succeeds
timeoutnumber The local timeout (in milliseconds) for the request
traditionalBoolean value whether to use the traditional style of param serialization
type string the type of request. (GET or POST)
urlstring URL to send the request to. Default is the current page
username stringa username to be used in an HTTP access authentication request
xhrfunction A function used for creating the XMLHttpRequest object

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.ajax({url: "demo_ajax_load.txt", async: false, success: function(result){
            $("div").html(result);
        }});//from ww w  .  j a v a 2 s.co m
    });
});
</script>
</head>
<body>

<div><h2>Let AJAX change this text</h2></div>

<button>Change Content</button>

</body>
</html>

Related Tutorials