AJAX--get/post方法

  • GET - 从指定的资源请求数据
  • POST - 向指定的资源提交要处理的数据

1、get()

语法:

$.get(URL,callback); //url可以是接口路径、也可以是资源文本路径

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
    </script>
    <script>
        $(document).ready(function(){
            $("button").click(function(){
                $.get("/try/ajax/demo_test.txt",function(data,status){
                    alert("数据: " + data + "\n状态: " + status);
                });
            });
        });
    </script>
</head>
<body>

<button>发送一个 HTTP GET 请求并获取返回结果</button>

</body>
</html>

2、post()

语法:

$.post(URL,data,callback);

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		$.post("/try/ajax/demo_test_post.php",{
			name:"菜鸟教程",
			url:"http://www.runoob.com"
		},
		function(data,status){
			alert("数据: \n" + data + "\n状态: " + status);
		});
	});
});
</script>
</head>
<body>

<button>发送一个 HTTP POST 请求页面并获取返回内容</button>

</body>
</html>
发布了252 篇原创文章 · 获赞 5 · 访问量 7812

猜你喜欢

转载自blog.csdn.net/qq_32603969/article/details/103889988