我在使用ajax在页面加载上显示内容时遇到了麻烦。ajax正在各自的控制器中调用正确的操作。我更新数据库的动作代码的第一部分运行良好。但我打电话给renderPartial的那部分不起作用。
**编辑*
下面是控制器操作::
public function actionUpdateProductData() {
Yii::import('application.components.DataScraper.*');
require_once('GetProductData.php');
$productRealTime = RealTime::model()->findAll();
if (count($productRealTime) === 0) {
$symbolData = new GetProductData();
$symbolData->getAmazonProductData();
} else {
echo CJSON::encode( array(
'status' => 'OK',
'div' => $this->renderPartial('_productDataGrid', array(
'model' => $productRealTime),
true, true ),
));
}
}如果部分工作正常。但其他部分不起作用。
以下是index.php视图::
<?php
/*
* Include the ajax Stock update script
*
*/
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl . '/js/ajaxProductDataUpdate.js');
?>
<div>
<hr>
<ul class="breadcrumb">
<li>
<a href="#">Home</a> <span class="divider">/</span>
</li>
<li>
<a href="#">Product Info</a>
</li>
</ul>
<hr>
</div>
<div class="span-10">
<div id="section2">
</div>
</div>下面是部分视图文件_productDataGrid.php
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'real-time-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'name',
'category',
'price'
'rating'
array(
'class' => 'bootstrap.widgets.TbButtonColumn',
),
),
));
?>下面是发出ajax请求的jQuery文件
var productParameters = {
ajaxUpdate: function() {
$.ajax({
url: "/ProductAnalysis/index.php/realTime/updateProductData",
type: "GET",
dataType:"json",
error: function(xhr, tStatus, e) {
if (!xhr) {
alert(" We have an error ");
alert(tStatus + " " + e.message);
} else {
alert("else: " + e.message); // the great unknown
}
},
success: function(data) {
$.fn.yiiGridView.update('real-time-grid', {
data: $(this).serialize()
});
}
});
}
};
$(document).ready(function() {
productParameters.ajaxUpdate();
});在加载页面/realTime/index.php时,我得到一个错误,上面写着
else:
undefined显然ajax调用失败了,但我不知道如何修复它。同样在Firebug中,控制器中的echo date()函数正在工作,但是Gridview无法工作。
请提供一些关于如何解决这个问题的帮助。告诉我我哪里做错了。我似乎无法在这方面取得任何进展。
提前谢谢你,
马克克斯
发布于 2013-10-21 07:05:48
看来,gridview小部件无法使用findall()。因此,我将数据提供程序更改为简单的模型,它现在起作用了。
以下是工作代码:
public function actionUpdateStockData() {
Yii::import('application.components.DataScraper.*');
require_once('GetStockData.php');
$productRealTime = new RealTime();
if (count($productRealTime->model()->findAll()) === 0) {
$symbolData = new GetproductData();
$symbolData->getAmazonProductData();
} else {
echo CJSON::encode( array(
'status' => 'success',
'div' => $this->renderPartial('_productDataGrid', array(
'model' => $productRealTime),
true, true ),
));
}
}发布于 2013-10-20 18:11:06
您的actionUpdateStockData()正在回显实际JSON内容编码之前的日期。因此,您没有传输正确的JSON,而XHR将失败。
删除echo date ...行,您应该会没事的。就像你正在做的一样--你应该为count(RealTime::model()->findAll()) === 0的情况添加一些响应。
https://stackoverflow.com/questions/19480534
复制相似问题