haiying520

jQuery简单的Ajax调用示例

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function(){
//按钮单击时执行
$("#testAjax").click(function(){
//取Ajax返回结果
//为了简单,这里简单地从文件中读取内容作为返回数据
htmlobj=$.ajax({url:"/Readme.txt",async:false});
//显示Ajax返回结果
$("#myDiv").html(htmlobj.responseText);
});
});
</script>
</head>
<body>
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="testAjax" type="button">Ajax改变内容</button>
</body>
</html>

 

当然,我们还可以这样来调用Ajax:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function(){
//按钮单击时执行
$("#testAjax").click(function(){

//Ajax调用处理
var html = $.ajax({
type: "POST",
url: "test.php",
data: "name=garfield&age=18",
async: false

}).responseText;
$("#myDiv").html(\'<h2>\'+html+\'</h2>\');
});
});
</script>
</head>
<body>
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="testAjax" type="button">Ajax改变内容</button>
</body>
</html>

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function(){
//按钮单击时执行
$("#testAjax").click(function(){

//Ajax调用处理
$.ajax({
type: "POST",
url: "test.php",
data: "name=garfield&age=18",
success: function(data){
$("#myDiv").html(\'<h2>\'+data+\'</h2>\');
}
});

});
});
</script>
</head>
<body>
<div id="myDiv"><h2>通过 AJAX 改变文本</h2></div>
<button id="testAjax" type="button">Ajax改变内容</button>
</body>
</html>

注意,

success: function(data)

中的data参数可以改为别的名称,比如success: function(msg),msg(data)为返回的数据。 此处为回调函数的参数,而非

data: "name=garfield&age=18"

中的Ajax调用中的data参数。

 

后台代码:

<?php
    $msg=\'Hello,\'.$_POST[\'name\'].\',your age is \'.$_POST[\'age\'].\'!\';
    echo $msg;
发表于 2016-03-30 15:48  西门飘雪001  阅读(292)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-16
  • 2021-08-09
  • 2021-12-16
  • 2021-12-15
  • 2021-05-20
  • 2022-12-23
  • 2021-06-19
猜你喜欢
  • 2021-11-19
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案