jQuery AJAX更新下拉选择onChange返回一个空字符串

时间:2013-12-01 22:43:40

标签: php jquery ajax codeigniter post

有人可以告诉我这里做错了什么,console.log()会给我一个空字符串而不是数组吗?

我想在第一个选择框上触发onChange后更新第二个选择框但我无法检索数据。当我做var_dump($results)时,它成功显示了一系列项目,但return $results返回一个空字符串。

这是它的样子:
enter image description here

javascript:

function _getNestedSelectOptions(activeComponent){
    $.ajax({
        type:   "POST",
        url:    "/backend/categories/get_nested_options/" + activeComponent
    }).done(function(html){
        console.log(html);
    });
}

这是一个php代码controllers/backend/categories.php

public function get_nested_options($cid = FALSE){
    if($cid == FALSE)
        $cid = $this->activeComponent;

    $categoriesResult = $this->categories_model->get_categories_tree_by_com($cid);

    $categoriesDropdownOptions = array();

    $this->categories_model->get_nested_options($categoriesDropdownOptions, $categoriesResult);

    var_dump($categoriesDropdownOptions); // <-- THIS WORKS AND SHOWS AN ARRAY OF ITEMS
    //return $categoriesDropdownOptions; // <-- THIS DOES NOT WORKING
}

这是console.log()上的输出:

数组 (size = 3)
  7 =&gt;字符串'管理员' - &gt; (长度= 14)
  8 =&gt;字符串'经理' - &gt; (长度= 8)
  9 =&gt;字符串'用户' - &gt; (长度= 5)

2 个答案:

答案 0 :(得分:1)

您可以尝试在js中获取json,使用

在控制器中:

public function getNestedOptions($cid = FALSE){
    if($cid == FALSE)
        $cid = $this->activeComponent;

    $categoriesResult = $this->categories_model->get_categories_tree_by_com($cid);
    $categoriesDropdownOptions = array();
    $this->categories_model->getNestedOptions($categoriesDropdownOptions, categoriesResult);
     echo json_encode($categoriesDropdownOptions); exit;
}



$.ajax({
  dataType: "json",
  type:   "POST",
  url:    "/backend/categories/get_nested_options/" + activeComponent
  data: data  
}).done(function(data){
    response = jQuery.parseJSON(data);
    console.log(response);
}); 

您将获得json格式的数据。

答案 1 :(得分:0)

我设法通过@KA_lin的帮助(jQuery.parseJSON()没有工作,idk为什么)和@RameshMhetre来解决这个问题,所以谢谢你们的帮助。

这是categories.js中的AJAX:

function _getNestedSelectOptions(activeComponent, targetName){
    // Url in proper JSON format with datas
    var url = "/backend/categories/get_nested_options/";
    // Target selectbox to apply modifications on it
    var ddb = $(targetName);
    // Save first dummy/null option
    var top = ddb.find('option:first');
    $.ajax({
        type: 'POST',
        url: url + activeComponent,
        dataType: 'json',
        beforeSend: function(){
            // Disable selectbox
            ddb.prop('disabled', true);
        },
        success: function(data){
            // Insert saved first dummy/null option
            ddb.empty().append(top);
            $.each(data, function (index, value) {
                // Append html data to target dropdown element
                ddb.append('<option val="'+ value.id +'">'+ value.title +'</option>');
            });
            // Re-enable selectbox
            ddb.prop('disabled', false);
        }
    });
}

controllers/backend/categories.php

public function get_nested_options($cid = FALSE){
    if($cid == FALSE)
        $cid = $this->activeComponent;

    // Get an array of properly constructed parents and childs
    $categoriesResult = $this->categories_model->get_categories_tree_by_com($cid);

    // Prepare an array of dropdown option values (without keys)
    $categoriesDropdownOptions = array();

    // Fill in an array with appropriate values for use with form_dropdown($array)
    $this->categories_model->get_nested_options($categoriesDropdownOptions, $categoriesResult);

    // Prepare an array for JSON output (with keys)
    $results = array();

    // Modify a raw array with specific keys for use in JSON
    foreach($categoriesDropdownOptions as $id => $title){
        $results[] = array(
            'id' => $id,
            'title' => $title
        );
    }

    // Set a proper page content and output JSON results
    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($results));
}

views/backend/categories/form.php

<fieldset>
    <legend>Attributes</legend>
    <label class="control-label" for="formCatCid">
        Component name:
        <div class="controls">
            <?php
                $componentDropdownExtra = 'class="span12" onChange="categories.getNestedSelectOptions(this.value, formCatPid);"';
                echo form_dropdown('formCatCid', $componentDropdownOptions, set_value('formCatCid', $formCatCid), $componentDropdownExtra);
            ?>
        </div>
    </label>
    <label class="control-label" for="formCatPid">
        Category parent:
        <div class="controls">
            <?php
                $categoriesDropdownExtra    = 'class="span12"';
                echo form_dropdown('formCatPid', $categoriesDropdownOptions, set_value('formCatPid', $formCatPid), $categoriesDropdownExtra);
            ?>
        </div>
    </label>
</fieldset>