jquery.ajax基础教程

一.jquery的引入.

 先下载文件:http://jquery.com/download/

然后加载jquery:

<!DOCTYPE html>
<html>
<head>
<script src="jquery文件的地址"></script>
</head>
<body>
</body>
<html>

 这样就加载了.

 

二.jquery.load(). 该方法从服务器加载数据,并把返回的数据放入被选元素中。

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">//引入jquery.
</script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    $('#test').load('/example/jquery/demo_test.txt');
  })
})
</script>
</head>

<body>

<h3 id="test">请点击下面的按钮,通过 jQuery AJAX 改变这段文本。</h3>
<button id="btn1" type="button">获得外部的内容</button>

</body>
</html>

 执行的地方http://www.w3school.com.cn/tiy/t.asp?f=jquery_ajax_load

 实例中的demo_test.txt'文件:

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>

 

三.jquery的get/post请求.

两种在客户端和服务器端进行请求-响应的常用方法是:GET 和 POST。

     GET - 从指定的资源请求数据.

GET 基本上用于从服务器获得(取回)数据。注释:GET 方法可能返回缓存数据。

     POST - 向指定的资源提交要处理的数据

POST 也可用于从服务器获取数据。不过,POST 方法不会缓存数据,并且常用于连同请求一起发送数据。

  1.  $.get(URL,callback);

URL:请求的地址.

callback:回调函数.(可选)

 2.$.post(URL,data,callback);

URL:请求的地址.

data:规定连同请求发送的数据(也就是传参).(可选)

callback:回调函数.(可选).

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.post("/example/jquery/demo_test_post.asp",
    {
      name:"Donald Duck",
      city:"Duckburg"
    },
    function(data,status){
      alert("数据:" + data + "\n状态:" + status);
    });
  });
});
</script>
</head>
<body>

<button>向页面发送 HTTP POST 请求,并获得返回的结果</button>

</body>
</html>

 执行地址:http://www.w3school.com.cn/tiy/t.asp?f=jquery_ajax_post.

我这里所讲的知识都可以在这个网站找到:http://www.w3school.com.cn/jquery/jquery_ajax_get_post.asp

猜你喜欢

转载自975156298.iteye.com/blog/2317579