服务器端页面地址:
http://localhost/basic/web/index.php?r=site/ajax&bag=nothing
bag 是个参数
注意,测试的时候前端html页面一定也要放在服务器上通过web服务器调用,这样前端页面和服务器端页面是在同一个web site下面。
// 这里是服务器端页面
public function actionAjax($bag = "nothing"){
echo $bag;
}
// 这里是前端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("span").click(function(){
txt = $(this).text();
$.get("http://Localhost/basic/web/index.php",{r:"site/ajax",bag:txt},function(result,status){
alert(result);
});
});
});
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo"><span id="ka">A</span> <span id="ke">Paragraph</span></p>
</body>
</html>