【问题标题】:Getting an Ajax response from Zend Framework Controller从 Zend Framework Controller 获取 Ajax 响应
【发布时间】:2012-06-07 04:35:13
【问题描述】:

我正在对控制器的一个视图执行 Ajax 请求,但我无法向 Ajax 方法发回响应。在下面的 sn-p 中,我试图将“hellopanda”这个词发回,但在警报消息中,我将获取数据作为对象。

查看:

$.ajax({  

      type: "POST",  
      url: "localhost/some-activity",
      data: dataString, 
      success: function(data) { 
              alert( "Data is: " + data);
          //do something with data
      },
      error: function(data){
               alert( "Data is: " + data);
        //do something with data 
          },
      onComplete: function(){

          }
    });

控制器:

 public function someActivityAction(){

   //do stuff

   echo "hellopanda";

 }

我很确定回声是问题所在。任何有关如何对视图做出正确响应的见解将不胜感激。

【问题讨论】:

    标签: jquery ajax zend-framework response


    【解决方案1】:

    您的网址是 "localhost/some-activity" 将其更改为 "localhost/someactivity" 在你的行动中

    public function someactivityAction(){
    
    //do stuff
    
    echo "hellopanda";
    
    exit;
    
    }
    

    如果你想返回一个数组,请确保编码像

    echo $this->_helper->json($yourarray);exit;
    

    你的ajax会是这样的

    $.ajax({  
    
          type: "POST",  
          url: "localhost/someactivity",
          data: dataString, 
          dataType: 'json', //if you are returning array
          success: function(data) { 
               for(int i=0;i<data.length;i++){
                     alert(data[i]['yourindexofarray']);
                  }
          },
          error: function(data){
                   alert( "Data is: " + data);
            //do something with data 
              },
          onComplete: function(){
    
              }
    });
    

    【讨论】:

    • 您能解释一下如何从 Zend 控制器中的 ajax 调用中获取变量吗?我需要在 PHP 中与从我的 ajax 发送的data 进行交互。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2017-03-07
    • 1970-01-01
    • 2014-10-29
    • 2017-12-12
    • 2019-05-07
    • 2012-06-06
    相关资源
    最近更新 更多