【发布时间】:2015-04-03 19:51:20
【问题描述】:
我为此苦苦挣扎。
我想在 CI 模型中发送一个 ID 并通过 CI 控制器获取返回值
我的看法是
<script type="text/javascript">
function showsome(){
var rs = $("#s_t_item option:selected").val();
var controller = 'items';
var base_url = '<?php echo site_url(); ?>';
$.ajax({
url : base_url+ '/' + controller+ '/get_unit_item',
type:'POST',
contentType: 'json',
data: {item_id: rs},
success: function(output_string){
//$('#result_area').val(output_string);
alert(output_string);
}
});
}
</script>
我的控制器方法是
public function get_unit_item()
{
$received = $this->input->post('item_id');
$query = $this->units_model->get_unit_item($received);
$output_string = '';
if(!is_null($query)) {
$output_string .= "{$query}";
} else {
$output_string = 'There are no unit found';
}
echo json_encode($output_string);
}
而我的模型功能负责
public function get_unit_item($where){
$this->db->where('item_id',$where);
$result = $this->db->get($this->tablename);
if($result->num_rows() >0 ){
$j = $result->row();
return $j->unit_item_info ;
}
}
HTML代码
<?php $id = 'id="s_t_product" onChange="showsome();"';
echo form_dropdown('product_id[]', $products, $prod,$id); ?>
我尝试仅使用 id 但未能触发,因此传递函数 onchange 似乎选择了项目并触发
使用 firebug 我可以看到 post 请求发送 item_id=2 但响应长度为 0 并且 php 结果代码为 302
发布 回复
我怎样才能做到这一点?(模型加载到构造器上)
【问题讨论】:
-
您向我们展示了 getprice 的请求,但是您发布的 ajax 代码向 get_unit_item 发出了请求。请向我们发送一致的信息。
-
更新,我认为从屏幕截图中您可以看到请求成功
-
@NaszNjokaSr。发布您的模型代码,并且您的
if条件必须类似于 if(!empty($query)) 或类似的东西,因为您可以看到您的 if 条件没有任何意义跨度> -
@NarendraSisodia 已更新,但结果相同。从返回值来看,调用似乎被重定向到登录页面
标签: php jquery ajax codeigniter