我有一个下拉列表,每当值发生变化时,我都会调用jacascript函数来更改某些值。我正在使用codeigniter,并在javascript中我使用了ajax,并对控制器函数进行了ajax调用。响应是“成功”,但它是空的,即使我在控制器中写回“hi”,响应也是空的。
ajax_operations.php
function getInventory()
{ echo "hi";
$table ="inventory"
$inventoryRecordOptionsData = "";
$inventoryRecords = array();
$option ="item_name";
if($this->input->post('storeId') != "" ){
//$inventoryRecords = $this->base_model->fetch_records_from($table, array(store_id => $this->input->post('storeId')));
$query = $this->db->query("SELECT id, item_name FROM dt_inventory WHERE store_id = 34");
$inventoryRecords = $query->result_array();
//$inventoryRecords = $this->db->get_where($table,array('store_id' => '35')->result();
if(count($inventoryRecords) > 0) {
$inventoryRecordOptionsData = '<option value="">Select '.$option.'</option>';
foreach($inventoryRecords as $ic) {
$inventoryRecordOptionsData = $inventoryRecordOptionsData . '<option value="'.$ic->id.'">'.$ic->item_name.'</option>';
}
}
else{
$inventoryRecordOptionsData='<option value="">Select '.$option.'</option>';
}
}
echo $option;
//echo $inventoryRecordOptionsData;
}
javascript功能
function getInventoryRecords(store_id)
{ //alert(store_id);
var childId = "items";
var optionTxt = "items";
if(store_id > 0) {
$.ajax({
type: "post",
url: "<?php echo site_url();?>/ajax_operations/getInventory",
data: "storeId="+store_id+"",
success: function(data, textStatus, xhr) {
if(data) {
$('#'+childId).empty();
$('#'+childId).append(data);
} else {
console.log("hi"+data+"hello");
$('#'+childId).empty();
alert("empty response");
$('#'+childId).append('<option value="">No '+optionTxt+' available.</option>');
}
$('#'+childId).trigger("liszt:updated");
}
});
} else {
$('#'+childId).empty();
$('#'+childId).append('<option value="">Select '+optionTxt1+' First.</option>');
$('#'+childId).trigger("liszt:updated");
}
}
我获得“成功”作为状态。但数据是空的。