加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax的小例子

发布时间:2020-12-16 00:19:33 所属栏目:百科 来源:网络整理
导读:load !DOCTYPE htmlhtmlheadscript src="/jquery/jquery-1.11.1.min.js"/scriptscript$(document).ready(function(){ $("button").click(function(){ $("#div1").load("/example/jquery/demo_test.txt h2","data",function(responseTxt,statusTxt,xhr){alert

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读