问题描述
我有一个editcategory视图,该视图显示有关类别的详细信息,并在该视图的底部显示一个类别描述表.
I have an editcategory view which shows details about a category and also a table of category descriptions showing at the bottom of the view.
类别描述表具有两个用于编辑和删除的锚标记.我正在使用ajax传递要编辑的描述的ID.需要调用控制器中的一个函数,该函数将调用categorydescription模型并根据传递的id提取记录.控制器需要将catDescription数组反馈给ajax成功函数.
The category description table has two anchor tags for edit and delete. I am using ajax to pass the id of the description to be edited. A function in the controller needs to be called, which calls the categorydescription model and pulls the record based on the id passed.The controller needs to feed the catDescription array back to the ajax success function.
已经有一个隐藏的div,其中包含用于编辑说明的表单,但是值设置如下
There is already a hidden div that contains the form for editing the description but the values are set as follows
<label for="catlang_name">Name</label>
<input type="text" name="catlang_name" id="catlang_name" class="text ui-widget-content ui-corner-all" value="<?php echo set_value('catlang_name',$catDescription->catlang_name); ?>"/>
我的问题是如何在Ajax中获取响应并分配
My problem is how I can get the response back in Ajax and assign the
为了正常地将数据传递到视图,我可以在控制器内完成以下操作
For normally passing the data to the view i could have done the following from within the controller
$data['catDescription']=$this->CategoryModel->getCategoryDescriptionById($id)
$this->load->view('category/categoryEdit', $data,true);
这是从模型返回的数据:
This is the data returned from the model :
Array (
[0] => Array (
[catlang_id] => 1
[catlang_cat_id] => 10
[catlang_lang_id] => 2
[catlang_name] => french
[catlang_description] => test
)
[1] => Array (
[catlang_id] => 2
[catlang_cat_id] => 10
[catlang_lang_id] => 2
[catlang_name] => english
[catlang_description] => test
)
)
任何建议将不胜感激.
欢呼
推荐答案
要将数组传递回ajax函数,应将其作为JSON返回.
To pass the array back to the ajax function, you should return it as JSON.
在您的控制器中,而不是调用视图,请编写以下代码:
In your controller, rather than calling a view, write this:
$this->output->set_output(json_encode($array));
这将以ajax函数可以处理的JSON输出数组.我想您正在使用jQuery处理ajax,如果尚未使用jQuery,请这样做.
Which will output the array in JSON, that the ajax function can process. I suppose you're using jQuery for handling the ajax, and if you aren't yet using jQuery, do so.
$.ajax(
url: '<?php echo site_url() ?>category/categoryEdit.php'
dataType:'json',
success: function(data) {
$.each(data, function(index,value){
//process your data by index, in example
$("#catlang_name").val(value.catlang_name);
});
}
);
这篇关于codeigniter ajax,处理服务器响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!