如何在ZF2中向控制器发送值AJAX?

时间:2014-01-20 15:53:46

标签: jquery ajax zend-framework2

我有这个代码 // ajax脚本

    <SCRIPT>
    $('#grupo').on('change', function() {
        var opc=$(this).find(":selected").text(); 
        $.ajax({                
                url: "seleccionGrupos/1",
                type: "POST",
                cache: false,
                ifModifiedBoolean:false,
                success:function (data){
                    $("#tablaGrupos").show();
                      //value from de CONTROLER
           $("#tablaGrupos").html('<?php echo $this->datostablagruposb ?>');

                },
                error: function(data) {
                   $("span").append("Oops Something Went Wrong");

                }
        });
    });

    </SCRIPT>

我需要动态查询结果如何才能得到它?任何解决方案 动态获取查询值

1 个答案:

答案 0 :(得分:0)

你的ajax电话:

<script type="text/javascript">
$(function(){
      $('#myFormID').bind("submit",function(event) {
      event.preventDefault();
      $.ajax({
            url     :$(this).attr("action"),// or the ure of your action
            type    : $(this).attr("method"),// POST, GET
            cache   : false,
            data    : $(this).serializeArray(), // the data from your form , in your case the :selected
            success : function( response, status,jQXHR) 
                     {
                    var result= $.parseJSON(response);// this is needed to decode your JSON data the you get get back you action 


                    },

            error   : function(jqXHR, textStatus, errorThrown){
                                 alert('Error: '+ errorThrown);
                              }
        });

        return false;
      });
});

</script>

您的操作在控制器中应如下所示:

<?php
 public function myAction()
    {        
        // get your form from your entity or model
           $form = $this->getForm();

        $response = $this->getResponse();
        $request = $this->getRequest();
        // get some form element and do somthing with theme her

        if ($request->isPost()){
            $form->setData($request->getPost());

        $response->setContent(\Zend\Json\Json::encode(array('data'=>'send any data you wish')));
        return $response;

    }
?>

你可以通过使用$('myButton')。target.attr('href')而不是post;来发送路线的ajax调用。希望这有帮助。