通过 Symfony2 控制器从数据库中提取记录到 javascript [英] Pulling records from database to javascript by Symfony2 controller

查看:26
本文介绍了通过 Symfony2 控制器从数据库中提取记录到 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的数据库中获取类别列表并将其放入我的 javascript 代码中,以便我以后可以使用它.但是我在这个任务中遇到了问题——在将这个列表返回给 javascript 之后——它们是空的.

Im trying to fetch categories list from my database and put it in my javascript code so i can use it later. But I've encountered problems with this task - after returning this list to javascript - they are empty.

这是我的 symfony2 控制器操作代码:

Here is my symfony2 controller action code:

public function fetchCategoriesAction(){
     $categories = $this->getDoctrine()->getRepository('MyDataBundle:Category')->findAll();
     $return=array("responseCode"=>200,  "categories"=>$categories);
     $return=json_encode($return);//jscon encode the array
     return new Response($return,200,array('Content-Type'=>'application/json'));
}

这是我的js代码:

var 类别;

函数类别Load(){

function categoriesLoad(){

var url=$("#categories_fetch").val(); //link to my controller

$.post(url,function(data){

     if(data.responseCode==200 ){           
         categories = data.categories;
         console.log(categories);
     }else{
       console.log("An unexpeded error occured.");
    }
});

}

我在跑步<代码>$(document).ready(function() {类别加载();});

但是在使用 console.log(categories) 之后我得到了空对象,尽管它们的数量与数据库中的记录数量相匹配.

But then after using console.log(categories) I'm getting empty objects, although their number match the number of records in the database.

我刚刚开始在 symfony2 中编程,希望得到任何帮助 :)

I'm just starting programming in symfony2 and I'd appreciate any help :)

已解决

我刚刚更改了我的控制器操作代码.这里更新了:

I've just changed my controller action code. Here it is updated:

public function fetchCategoriesAction(){

    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new GetSetMethodNormalizer());
    $serializer = new Serializer($normalizers, $encoders);

    $em = $this->getDoctrine()->getManager();
    $categories = $em->createQuery(
            'SELECT u
            FROM MyDataBundle:Category u'
    )->getResult();

    $categories = $serializer->serialize($categories, 'json');
    $return=array("responseCode"=>200,  "categories"=>$categories);
    $return=json_encode($return);
    return new Response($return,200,array('Content-Type'=>'application/json'));
}

现在一切正常.感谢@Pawel 和@SAM

Now it works fine. Thanks to @Pawel and @SAM

推荐答案

我的例子:PHP函数:

My example: PHP function:

public function getDistrictListAction($id) {

        $em = $this->getDoctrine()->getManager();

        $query = $em->createQuery(
           // DQL
        );

        return new JsonResponse($query->getArrayResult());
    }

JS代码:

var dropdown = $('.dropdown-selector');
dropdown.change( function() {
    $.ajax({
         url: 'url_to_function'
         beforeSend: function() {
              dropdown.attr('disabled', true);
         },
         success: function(data) {
              dropdown.find('option').not('[value=]').remove();
              $.each( JSON.parse(data), function(key, value) {
                   dropdown.append( $( "<option></option>" ).attr( "value", value.id ).text( value.name ));
              });
              dropdown.attr('disabled', false);
         }
    });
}

您可以将更改事件设置为例如回调.在发送之前,您禁用下拉菜单,在通过 AJAX 选项加载后,您将再次启用它.

This you can set on change event for example as a callback. Before send you make dropdown disabled, and after load via AJAX option you are enabale it again.

这篇关于通过 Symfony2 控制器从数据库中提取记录到 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆