jQuery get()方法
在jQuery中.get()方法通过使用GET HTTP请求从服务器加载数据。这个方法返回XMLHttpRequest对象。
语法
$.get( url, [data], [callback], [type] )
参数
- url : 包含要发送请求的URL的字符串
- data:这是一个可选的参数,代表将被发送到服务器的键/值对。
- callback。这个可选参数表示每当数据被成功加载时要执行的函数。
- type。这个参数表示返回给回调函数的数据类型,即 “xml”、”script”、”json”、”html”、”jsonp “或 “text”。
示例:
- 这个PHP代码是用来在下面的html程序发送HTTP GET请求时获取数据的。
// Result.php file
<?php
if( _REQUEST["name"] ) {
name = _REQUEST['name'];
echo "Welcome ".name;
}
?>
- 这个HTML代码用于发送HTTP GET请求。
<html>
<head>
<script type="text/javascript"
src=
"https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type="text/javascript"
language="javascript">
(document).ready(function() {
("#driver").click(function(event) {
.get(
"result.php", {
name: "GFG"
},
function(data) {
('#stage').html(data);
});
});
});
</script>
</head>
<body>
<p>Click on the button to load result file
</p>
<span id="stage"
style="background-color:#cc0;">
GeeksForGeeks
</span>
<div>
<input type="button"
id="driver"
value="Load Data" />
</div>
</body>
</html>
输出:
在点击按钮之前:
点击按钮后:。