【问题标题】:cannot get ajax responseText with Phalcon无法使用 Phalcon 获取 ajax responseText
【发布时间】:2015-10-20 12:55:55
【问题描述】:

我想调用ajax 并显示它的响应:

<script type="text/javascript">
function test() {
    var pk = $('#salle_code').val();
    var donne = {pk:pk};
    var ret = $.ajax({
            data: donne,
            type: "POST",
            url:  "<?php echo HTTP_AJAX ?>salle/testAjax.php",
            async: false
         }).responseText;
    return $.trim(ret);
}
$(document).ready(function(){
    $('#salle_code').on("blur", function() {
        if ($('#salle_code').val() != "") {
            alert(""+test());
        }
    });
});
</script>

ajax 代码:

<?php
$critere = array();
$critere['salle_code'] = $_POST['pk'];
$ret = Salle::lireParCritere($critere);
echo "111111111111111";
?>

在运行时警报显示空白结果!那么如何使用 Phalcon 和 ajax 以及模型呢?

【问题讨论】:

    标签: php ajax phalcon


    【解决方案1】:

    @Klay 有一个非常简单的解决方案:我在实际视图的控制器内创建了一个 ajax 操作。

    【讨论】:

      【解决方案2】:

      首先您需要为 AJAX 请求定义一个路由,例如/salle/test:

      $router->add('/salle/test', [
          'controller' => 'salle',
          'action' => 'test',
      ))->beforeMatch(function ($uri, $route) {
          if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest') {
              return false;
          }
          return true;
      });
      

      然后创建您的操作:

      public function testAction()
      {
          // some work ..
      
          $this->response->setJsonContent(json_encode(['foo' => 'bar']));
          return $this->response;
      }
      

      然后测试:

      <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
      <script type="text/javascript">
          function test() {
              var response = $.ajax({
                  type: "POST",
                  data: {},
                  url: '/salle/test',
                  success:function(results) {
                      console.log(results);
                  }
              });
      
              return response;
          }
          $(document).ready(function(){
              console.log(test());
          });
      </script>
      

      【讨论】:

        【解决方案3】:

        使用以下代码并检查浏览器控制台的响应

         $.ajax({
            data: donne,
            type: "POST",
            url:  "<?php echo HTTP_AJAX ?>salle/testAjax.php",
            async: false
            success: function (data) {
                console.log(data)
            },
            error: function (textStatus, errorThrown) {
               console.log(textStatus + " : " + errorThrown)
            }
         });
        

        【讨论】:

          猜你喜欢
          • 2016-03-24
          • 2018-10-21
          • 1970-01-01
          • 2020-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多