我目前正在使用以下代码向我的控制器发送 Ajax get 请求:
echo CHtml::ajaxLink('clickMe', array('ajax'), array('update'=>'#results'));
这工作正常,控制器接收请求并相应地更新视图。
现在,我想发送模型的此请求属性,即从model->getAttributes();
我应该怎么做?创建属性的 JSON 对象并将其与请求一起发送?
如果需要,只需传递"data"属性和"type":
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'update' => '#results'
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
此代码只是将 #results 内容替换为 json。如果您需要不同的东西,请使用"成功"而不是"更新",如下所示:
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'success' => 'function (response) {
// do everything you need
}',
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
查看 jquery ajax 选项以获取更多信息。