![]() |
VOOZH | about |
In this article, we will see the parameters that are used in the jQuery Ajax method along with understanding their implementations through the illustrations.
The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Generally, the ajax() method is used in all the required jQuery AJAX methods, which are mostly used for requests & which is not being used by the other methods.
Syntax:
$.ajax({name:value, name:value, ... })
Parameter Values: The list of possible values are given below:
The various parameters can be understood by implementing the following examples. Few are given for reference but the developer can change code as per the need.
Example 1: The following code demonstrates the ajax() method with "url" and "context" parameters which adds a class to the body document for changing the background color by using CSS background-color property. The class is added by using the jQuery addClass() method. The value of "url" will be the "test.html" file provided below the code.
test.html: This file is used in the above file.
<html> <p>hello world</p> </html>
Output:
Example 2: The following code demonstrates the ajax() method with "success" and "error" parameters. The value of "url" will be the "sample.html" file provided below the code.
sample.txt: This file is used in the above file.
This is changed paragraph for the HTML document.<br> There are no such pre-requisites required to understand the latter portion of the article.<br> Only the basic knowledge of HTML, CSS, and Javascript are good to go.
Output:
Example 3: The following code demonstrates the ajax() method with "contentType", "dataType" and beforeSend() method parameters. The value of "url" will be the "url.json" file provided below the code.
url.json: This JSON file is used in the above file.
{
"date": "07-15-2022",
"name": "Ajax learning",
"time": "11:32:10 AM"
}
Output:
Example 4: The following code demonstrates the ajax() method with "contentType", "dataType" and "jsonpCallback" parameters. The value of "url" will be the "urljsonP.json" file provided below the code. Set the jsonpCallback: 'JSONPResponse' propertywhere "JSONPResponse" is mentioned in the JSON file as shown below.
urljsonP.json: This JSON file is used in the above file.
JSONPResponse( {
"id": 1,
"name": "accessory",
"items": [ "book", "pen" ]
})
Output: