微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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"
    },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

原文地址:https://www.jb51.cc/ajax/164287.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐