duanli0119 2013-07-06 07: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 08: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 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国