通过Cakephp中的ajax发送数据 [英] sending data via ajax in Cakephp

查看:122
本文介绍了通过Cakephp中的ajax发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的cakephp,并尝试从ajax发送数据到我的控制器操作
i有一个弹出模型,其中有一个输入框..我想抓住该值,发送到控制器没有页面刷新

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

这里是我的代码..

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

我的javascript

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;
    }
}



我想先获取值,然后发送对ajax请求的响应

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

推荐答案

它简单的post到控制器的值,并做你想要的,在ajax请求绑定值 data:{value_to_send:value} 并获取控制器

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

这篇关于通过Cakephp中的ajax发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆