📅  最后修改于: 2020-11-26 07:14:02             🧑  作者: Mango
AJAX是异步JavaScript和XML的首字母缩写。它是一组相互关联的技术,例如JavaScript,DOM,XML,HTML / XHTML,CSS,XMLHttpRequest等。它使我们能够异步发送和接收数据而无需重新加载网页。所以它很快。
jQuery中的ajax()方法执行AJAX请求。它将异步HTTP请求发送到服务器。 JQuery提供了一组丰富的AJAX方法来开发Web应用程序。它被广泛用于请求。
使用ajax()方法的语法如下。
$.ajax({name:value, name:value, ... })
该方法可以为AJAX请求提供多个名称/值对。名称和值在下表中定义。
Name | Value |
---|---|
async | It is a Boolean value. Its default value is true, which means that by default, the request is handled asynchronously. For synchronous requests, we can set it to false. |
beforeSend(xhr) | It is a callback function that executes before a request is sent. |
cache | It is a Boolean value. Its default value is true. It represents whether the browser cache the requested pages. On setting it to false, it forces the pages not to be cached by the browser. |
complete(xhr, status) | It is a callback function executed when the request is finished. It has two arguments that are xhr (XMLHttpRequest) and a status. The status can be “success”, “notmodified”, “nocontent”, “error”, etc.It is a callback function executed when the request is finished. It has two arguments that are xhr (XMLHttpRequest) and a status. The status can be “success”, “notmodified”, “nocontent”, “error”, etc. |
contentType | It is used when sending the data to the server. Its default value is “application/x-www-form-urlencoded”. |
data | It is data sent to the server. It could be a string, an array, or a JSON object. |
dataFilter(data, type) | It is a function that handles the raw response data of the XMLHttpRequest. It accepts two parameters. |
dataType | It is the type of data we are expecting from the server. |
error(xhr, status, error) | It is a callback function that gets executed when a request fails. It accepts three arguments. |
global | It is a Boolean value. Its default value is true. It represents whether to trigger a global AJAX event handler or not. |
ifModified | It is a Boolean value. Its default value is false. It specifies a request to be successful if the response has changed since the last header. |
jsonp | It is a string that overrides the callback function name in a jsonp request. |
jsonpCallback | It is a string that contains the name of the callback function for a jsonp request. |
password | It is used to specify a password that is to be used in an HTTP access authentication request. |
processData | It is a Boolean value. Its default value is true. It is used to specify whether or not the data sent with the request should be converted into a query string. |
scriptCharset | It specifies the charset for the request. It applies only when the “script” transport is used. |
success(result, status, xhr) | As its name implies, this callback function executes when the request succeeds. It accepts three arguments. |
timeout | It is the timeout for the request in terms of milliseconds. If it is set to the value 0, it means that there is no timeout. |
traditional | It is a Boolean value. We can set it to true if we want to use the traditional style of param serialization. |
type | It specifies the type of http request, such as POST, PUT, and GET. Its default value is GET. |
url | It is the URL to which the request is sent. Its default value is the current page. |
username | It is a username that is to be used in an HTTP access authentication request. |
xhr | It is a callback function that is used for creating the XMLHttpRequest object. |
让我们通过一些插图来了解ajax()方法的使用。
下面给出了一些步骤。请按照以下步骤操作,以更清楚地了解ajax()方法的使用。
现在,让我们看看使用ajax()方法的代码。在此示例中,我们包括URL参数和可选的type参数。在此示例中,ajax()方法用于使用ajax请求添加内容。
在这里,我们将test.html传递给ajax()方法的URL参数。
test.html
Hello World
Welcome to the javaTpoint.com
Example2.html
This is an example of using the jQuery's ajax() method.
Click the following button to see the effect.
输出量
执行完上述代码后,输出将为-
点击给定的按钮,输出将是-
这是使用ajax()方法的另一个示例。在这里,我们使用可选的async属性,并为异步请求将其设置为false。
test.html
Hello World
Welcome to the javaTpoint.com
This is an example of using the jQuery's ajax() method.
Click the following button to see the effect.
输出量
执行完上述代码后,输出将为-
点击给定的按钮,输出将是-
这是使用ajax()方法的另一个示例。在这里,我们将.js文件传递给ajax()方法的URL参数。我们还使用可选的dataType参数并将其设置为脚本值。
test.js
alert("Welcome to the javaTpoint.com \n This alert is load by the AJAX");
Example2.html
This is an example of using the jQuery's ajax() method.
Click the following button to see the effect.
输出量
执行完上述代码后,输出将为-
点击给定的按钮,输出将是-
上面的示例将帮助您理解ajax()方法的概念。