使用YII,我试图从ajax调用的页面进行查询
echo CHtml::ajaxLink(
'Test request',
array('ajax/z1_gerar_excel.php',
'type'=>'POST',
'data' => array('sql' => $sql)
),
array('update'=>'#req_res'
)
);
?>
<div id="req_res">...</div>
这是AJAX页面:
$sql = $_REQUEST['data']['sql'];
//echo $sql;
$connection=Yii::app()->db;
$command = $connection->createCommand($sql);
$dataReader = $command->query();
$cont_l = 1;
while(($row = $dataReader->read()) !== false)
{
echo "<br>Linha $cont_l: ";
print_r($row);
$cont_l++;
}// WHILE EOF
$connection->active = false;
我有以下问题,YII类没有在从ajax调用的页面中初始化,所以我得到以下结果:
致命错误:班级&#39; Yii&#39;在第4行的D:\ Programas \ Xampp \ Instalado2 \ htdocs \ atayo-teste \ ajax \ z1_gerar_excel.php中找不到
我只使用YII一周。如何解决此错误?
更新:
所以我尝试将其更改为YII方式,我按照http://lostmahbles.com/simple-yii-ajaxlink/中的示例将其添加到我的视图(z1_sql.php)中:
echo CHtml::ajaxLink(
"Link Text",
Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
array( // ajaxOptions
'type' => 'POST',
'beforeSend' => "function( request )
{
// Set up any pre-sending stuff like initializing progress indicators
}",
'success' => "function( data )
{
// handle return data
alert( data );
}",
'data' => array( 'val1' => '1', 'val2' => '2' )
),
array( //htmlOptions
'href' => Yii::app()->createUrl( 'application/controllers/RelatorioController/ajaxRequest' ),
'class' => 'actionAjaxRequest'
)
);
这是我的控制器(RelatorioController.php):
public function accessRules()
{
return array(
array('allow',
'actions'=>array('ajaxrequest'),
'users'=>array('@'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionAjaxRequest()
{
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
echo "some sort of response";
Yii::app()->end();
}
但我得到404错误。再一次,真的没有进入YII,所以任何事情都有帮助...
答案 0 :(得分:0)
你没有要求Yii php功能/页面。 将您的代码更改为以下内容:(未经测试)
<?php
echo ajaxLink('text', array('ajax/z1_gerar_excel'), array(
'type' =>'POST',
'data' => array('sql' => $sql),
), array(
'update'=>'#req_res' // this array is for htmlOptions, so I think you want this parameter in the array with the ajax options
));
?>
确保您的ajax控制器具有函数z1_gerar_excel
。
有关ajaxLink
参数的更多信息,请查看:
http://www.yiiframework.com/doc/api/1.1/CHtml#ajaxLink-detail