duanli0119 2013-07-06 15:57
浏览 149
已采纳

通过Cakephp中的ajax发送数据

i am new to cakephp and trying to send data from ajax to my controller action.. i have a popup model in which there is a input box ..i want to grab that value and send to controller without page refresh

here is my code ..

 <a  class="button anthracite-gradient" onclick="openPrompt()">submit </a>

my javascript

 function openPrompt()
{
var cancelled = true;

$.modal.prompt('Please enter a value:', function(value)
{

    $.ajax({
        type:"POST",

        url:"/cakephp/controller/action/",
        success : function(data) {
           alert(value); //value right now is in this variable ... i want to send this variable value to the controller

        },
        error : function() {
           alert("false");
        }
    });


    }, function()
   {

    });
    };
</script>

myController

 public function action(){
    if( $this->request->is('ajax') ) {
      $new = $this->request->data; 

        echo "ok"
        return;
    }
}

i want to first get the value here and then send the response to may ajax request

  • 写回答

2条回答 默认 最新

  • dongyanfeng0546 2013-07-06 16:12
    关注

    Its simple post the value to the controller and do what you want , in ajax request bind the value in data:{value_to_send:value} and get in controller

     function openPrompt()
    {
    var cancelled = true;
    
    $.modal.prompt('Please enter a value:', function(value)
    {
    
        $.ajax({
            type:"POST",
            data:{value_to_send:value}, 
            url:"/cakephp/controller/action/",
            success : function(data) {
               alert(data);// will alert "ok"
    
            },
            error : function() {
               alert("false");
            }
        });
    
    
        }, function()
       {
    
        });
        };
    </script>
    
     public function action(){
        if( $this->request->is('ajax') ) {
         // echo $_POST['value_to_send'];
         echo   $value = $this->request->data('value_to_send');
    
         //or debug($this->request->data);
            echo "ok"
          die();
        }
       }
    

    For more see accessing-post-data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog