Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

Jquery get()方法

jQuery get()方法使用HTTP GET请求从服务器加载数据。它用于发出简单的GET请求。它返回 XMLHttpRequest 对象。

语法

$(selector).get(URL, data, function(data, status, xhr), dataType)

参数值

此方法包括四个参数值,其中一个是必需的,其他是可选的。我们必须在 get()方法中包含 URL 参数。下表中定义了 get()方法的参数值。
参数
URL 这是请求发送到的URL。这是必填参数。
data 这是一个可选参数。它是与请求一起发送到服务器的数据。它可以是普通对象或字符串。
function(data, status, xhr), dataType) 这也是一个可选参数。这是一个在成功的服务器请求上执行的回调函数。
它还具有三个参数,分别是data,status和xhr,其中数据包含来自请求的结果数据,status表示请求状态,例如"成功", "错误"等,并且xhr包含XMLHttpRequest对象。
dataType 这也是一个可选参数,用于定义我们期望服务器提供的数据类型。类型可以是"text"," json"," jsonp"," html","script"和" XML"。
现在,让我们看一个使用 get()方法的简单示例。

示例

在在此示例中,我们通过使用 get()方法发出一个get请求。我们正在使用 get()方法的两个参数,即 URL 和回调函数。
URL参数设置为值 test.html 。我们使用回调函数的两个参数,分别是 data status ,其中第一个参数保存数据
在输出中,我们可以看到从服务器加载的数据,并且请求的状态为 成功。
test.html
<h1> Hello World </h1>
<h2> Welcome to the lidihuo.com </h2>
Example2.html
<!DOCTYPE html>
<html>
<head>
<title> jQuery get() method </title>
<script src = "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
</head>
<body>
<h3> This is an example of using the get() method in jQuery </h3>
<p> Click the following button to see the effect. </p>
<button> Click me </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<script>
$(document).ready(function() {
$("button").click(function() {
$.get("test.html", function(data,status) {
document.getElementById("p1").innerHTML = data;
document.getElementById("p2").innerHTML = "Status: " + status;
});
});
});
</script>
</body>
</html>
输出

This is an example of using the get() method in jQuery

Click the following button to see the effect.

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4