Phalcon Ajax 响应 Returns HTML

Phalcon Ajax Response Returns HTML

所以我正在尝试实现这个: https://github.com/phalcon/cphalcon/wiki/Dependent-Select-Dropdown

我一切正常,它创建 selects,填充第一个 select,当我正确选择一个值时,returns 值如下所示,当我调用 grabStatesAction 时在控制器中:

[{"id":"2","name":"Section1"},{"id":"24","name":"Section2"}"}]

但是,如果我提醒 javascript 内的响应,它根本不会 return 而是 return 来自页面的一堆 HTML。

这是我的脚本

<script type="text/javascript">
    $("#id_product").change(function() {
            var value = $(this).val();

            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: '/admin/section/grabSection/',
                data: {"id": value},
                success: function(response){
                    $("#selectSection option")
                        .not(":first").remove();

                    alert(response);
                    parsed = $.parseJSON(response);


                    $.each(parsed, function(key, value) {
                        $("#selectSection")
                            .append($("<option></option>")
                            .attr("value",value.id)
                            .text(value.name));
                    });
                }
            });
        });
</script>

这是控制器

public function grabSectionAction()
    {
        $id=2; //hardcoded for testing purposes
        $data = Sections::find(array(
            'columns' => array('id_section, section'),
            'conditions' => 'active = 1 AND id_section = :id:',
            'bind' => array('id'=>$id)
        ));
        $resData = array();
        foreach ($data as $result) {
            $resData[] = array("id"=>$result->id_section, "name"=>$result->section);
        }

        echo json_encode($resData);
    }

带有 select 的页面是带有表单的模态 window。我在想,也许页面应该将应用程序类型设置为 json,也许这就是问题所在,但如果我这样做,那么表单就会中断。我确实将 javascript 中的应用程序类型设置为 json。任何想法我做错了什么,或者您需要任何特定的附加信息,请告诉我

在下面尝试.. 使用 Console.log 而不是 alert 来查看完整的响应字符串。 更多信息:https://developer.chrome.com/devtools/docs/console-api

public function grabSectionAction()
{
    $this->view->disable();

    //Create a response instance
    $response = new \Phalcon\Http\Response();

    $id = 2; //hardcoded for testing purposes
    $data = Sections::find(array(
        'columns' => array('id_section, section'),
        'conditions' => 'active = 1 AND id_section = :id:',
        'bind' => array('id' => $id)
    ));
    $resData = array();
    foreach ($data as $result) {
        $resData[] = array("id" => $result->id_section, "name" => $result->section);
    }

    //Set the content of the response
    $response->setContent(json_encode($resData));

    //Return the response
    return $response;
}