ajax的小例子

load

<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("/example/jquery/demo_test.txt h2","data",function(responseTxt,statusTxt,xhr){alert("this is a test"+responseTxt)});
  });
});
</script>
</head>
<body>

<div id="div1"><h2>使用 jQuery AJAX 来改变文本</h2></div>
<button>获得外部内容</button>
<pre>
     <h2>jQuery and AJAX is FUN!!!</h2>
     <p id="p1">This is some text in a paragraph.</p>
</pre>
</body>
</html>

放到http://www.w3school.com.cn/上执行吧

更多例子详见<div><a href="http://www.w3school.com.cn/jquery/jquery_ajax_load.asp">w2school</a></div>

get: 从指定的资源请求数据

语法:

$.get(URL,callback);

必需的 URL 参数规定您希望请求的 URL。

可选的 callback 参数是请求成功后所执行的函数名。第二个参数是回调函数。第一个回调参数存有被请求页面的内容,第二个回调参数存有请求的状态。

下面的例子使用 $.get() 方法从服务器上的一个文件中取回数据:


<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.get("/example/jquery/demo_test.asp",function(data,status){
      document.write("数据:" + data + "\n状态:" + status);
    });
  });
});
</script>
</head>
<body>

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

</body>
</html>

提示: 这个 ASP 文件 ("demo_test.asp") 类似这样:
<%
response.write("This is some text from an external ASP file.")
%>



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

$.post() 方法通过 HTTP POST 请求从服务器上请求数据。

语法:

$.post(URL,data,callback);

必需的 URL 参数规定您希望请求的 URL。

可选的 data 参数规定连同请求发送的数据。

可选的 callback 参数是请求成功后所执行的函数名。

下面的例子使用 $.post() 连同请求一起发送数据:


<!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/example/jquery/demo_test_post.asp内容:
     Dear . Hope you live well in .
执行之 后弹出框:



HTTP 方法:GET 对比 POST

详见:http://www.w3school.com.cn/tags/html_ref_httpmethods.asp




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值