view端
<?php
use common\widgets\JsBlock;
use yii\helpers\Url;
?>
<li class="mb5">
<label class="sectionLabel-A1">test*:
<a href="#" id="test" style="color: #3B5999;font-weight: normal">添加视角</a>
</label>
<div id="addAngles" class="sectionBox-A1 clear sectionForm-A1 sectionForm-A1-4">
</div>
</li>
<?php JsBlock::begin() ?>
<script>
var path = "<?php echo Url::toRoute('article/ajax',true); ?>";
$('#test').click(function()
{
$.ajax({
type: "POST",
url: path,
data:{
"Page": 1,
"Size": 8,
"Path": path,
"Children": false,
"Sort": "-Article__Time"
} ,
dataType: "json",
success: function(json){
$("#addAngles").append(json);
}
})
});
</script>
<?php JsBlock::end() ?>
controller 端
public function actionAjax(){
if (Yii::$app->request->isAjax) {
$para = Yii::$app->request->post();
$searchname= explode(":", $para['Page']);
$b = $para['Path'];
$test = "ajax worked!";
return \yii\helpers\Json::encode($b);
}
}
结果显示
test*: 添加视角
http://www.wanjia.com/frontend/web/index.php?r=article%2Fajax