douqianzha6213 2012-07-04 08:02
浏览 54

通过jquery ajax调用函数

It's been 2 days now and still I can't get this problem to work. So basically I am trying to call a php function through jquery ajax but nothing is working, I am not sure what is the problem is... here are my codes

the ajax

$(document).ready(function() {
    $("#idCheck1").click(function(){
        $.ajax({
            url:'../../../../Controller/PostsController.php', 
            data: {action: 'update_checkbox'},
            success:function(result){
//                $("#dsa").html(result);
            } 
        });
    });
});

the view

<?php echo $this->Form->create("Posts", array("action" => "update_checkbox", "id" => "checkingBox")) ?>
                    <td>
                        <?php
                        echo $this->Form->input('Post.' . $i . '.id', array("type" => "hidden", "label" => false, "value" => $sum['posts']['id']))
                        ?>
                        <?php
                        echo $this->Form->input('Post.' . $i . '.done', array("type" => "checkbox", "label" => false, "value" => "1", "id" => "idCheck1"))
                        ?>
                    </td>
                </tr>
                <?php
                $i++;
            }
            ?>

        </table>
        <?php echo $this->Form->end(); ?>

the controller

 public function update_checkbox() {
        //    debug($this->data);
        $var = $this->Post->saveCheckBox($this->data);
        $this->set("result", $var);
    }

the model

public function saveCheckBox($checkbox) {
        debug($checkbox);
        $this->saveAll($checkbox['Post']);
    }
  • 写回答

1条回答 默认 最新

  • doudilin1225 2012-07-04 08:27
    关注

    The url

    url:'../../../../Controller/PostsController.php'
    

    looks wrong. Since this is an AJAX request that goes through the browser you can't use relative paths that try to go upwards in the folder hierarchy, as browsers url's don't work that way. You should be making that request so that it is passed through a web server, i.e. the url should look like one of the following:

    url:'http://localhost/Controller/PostsController.php'
    

    or

    url:'/Controller/PostsController.php'
    

    The first option is an absolute url, but this also makes the code a bit less flexible (suppose you change the domain from localhost to something else). The second option is a relative URL, but one that is relative to the domain root of your web server (i.e. in the example it would still resolve to localhost/Controller...).

    In both cases based on what you've posted, your PHP file should live in a Controller/ folder in the document root of your site. The structure of your code however suggests that you are using a framework of some kind (e.g. Zend, Symfony or CodeIgniter)? If that is the case it would be helpful if you post information on what framework you're using as well as that might change the answer.

    UPDATE

    In the case of cakePHP, you should access the controller through the front controller, meaning the URL should look like this:

    url:'/posts'
    

    UPDATE 2

    For the jquery side, a complete ajax request example could look like this:

    $(".idCheck").click(
       function(){ 
          var idVal = $(this).val();
    
          $.ajax({ 
             url:'/posts/update_checkbox', 
             data: {id: idVal }, 
             type: 'POST', 
             success:function(result){ 
                $("#dsa").html(result); 
             } 
          }); 
      });
    

    Note that the URL already contains the update_checkbox action (i.e. the complete url to the action you want to execute), and the data object contains the value of the clicked element that you want to send to the server. If you want to send a complete form you could also use $('#myformselector').serialize() to convert all inputs in the form to a object suitable for the data property of the ajax request.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)