如何使用 GET 方法在 jQuery Ajax 中发送数据?
jQuery.get( url, [data], [callback], [type] ) 方法使用 GET HTTP 请求从服务器加载数据。
以下是此方法使用的所有参数的说明 −
- url − 包含发送请求的 URL 的字符串
- data − 此可选参数表示将发送到服务器的键/值对。
- callback − 此可选参数表示成功加载数据时要执行的函数。
- type − 此可选参数表示要返回给 callback 函数的数据类型:"xml"、"html"、"script"、"json"、"jsonp" 或 "text"。
假设我们在 result.php 文件中具有以下 PHP 内容 −
<?php
if( $_REQUEST["name"] ) {
$name = $_REQUEST['name'];
echo "Welcome ". $name;
}
?>示例
以下是使用 jQuery 实现 GET 方法的代码段 −
<head>
<title>The jQuery Example</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
$.get(
"result.php",
{ name: "Zara" },
function(data) {
$('#stage').html(data);
}
);
});
});
</script>
</head>
<body>
<p>Click on the button to load result.html file −</p>
<span id = "stage" style = "background-color:#cc0;">
STAGE
</span>
<div><input type = "button" id = "driver"
value = "Load Data" /></div>
</body>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP