Tag: cakephp

cakephp,jquery,.ajax(),dataType:json

是否可以在没有cakephp控制器function的情况下进行操作? 我试图让我的服务器返回一个不是字符串的数据类型 – 而是一个数组 我的控制器function: function test() { $this->layout = ‘plain’; $task[‘Numbers’][‘uno’] = ‘mooo’; $task[‘Numbers’][‘dos’] = ‘says the cow’; $result = json_encode($task); $this->set(‘result’, $result); } 我的视图文件test.ctp echo $result; echo $result; 我的jquery: $(‘#test’).live(‘click’, test); function test() { var data = $(‘#form’).serialize(); $.ajax({ type: “post”, url: “/controller/test”, data: data, dataType: ‘json’, success: function(response){ alert(response.Numbers.uno); } }); } […]

cakephp 2.3 ajax表单提交

我正在尝试创建一个简单的表单,使用ajax将数据提交到数据库。 这是我的看法 Html->script(‘jquery’, FALSE); ?> Html->script(‘validation’, FALSE); ?> Contact Us Form->create(); echo $this->Form->input(‘name’, array(‘id’=>’name’)); echo $this->Form->input(’email’, array(‘id’=>’email’)); echo $this->Form->input(‘message’, array(‘id’=>’message’)); echo $this->Js->submit(‘Send’, array( ‘before’=>$this->Js->get(‘#sending’)->effect(‘fadeIn’), ‘success’=>$this->Js->get(‘#sending’)->effect(‘fadeOut’), ‘update’=>’#success’ )); echo $this->Form->end(); ?> Sending… 调节器 class MessagesController extends AppController{ public $helpers = array(‘Js’); public $components = array(‘RequestHandler’); public function index(){ if(!empty($this->data)){ if($this->Message->save($this->data)){ if($this->request->isAjax()){ $this->render(‘success’,’ajax’); }else{ $this->Session->setFlash(‘Message sent’); $this->redirect(array(‘action’=>’index’)); […]

一种从$ .ajax中的url参数中删除附加url以获取jquery的方法

我正在使用cakephp,并希望使用jQuery.ajax发布到我的服务器。 问题是jQuery的$.ajax附加协议和主机名,但也包括当前控制器,所以我也无法更改我粘贴的控制器。 有没有办法更改$.ajax的url:参数? 这是我的示例邮政编码: $.ajax({ url: ‘controller/action’, type: ‘post’, data: $(‘#form_data’).serialize(), dataType: ‘json’, success: function(json) { console.log(json); } });

CakePHP和jQuery – 不引人注目的动作

我正试图在CakePHP中删除书签,这是一个不引人注目的动作。 虽然它工作得很好,但我怀疑必须有更好的方法来做到这一点。 有人可以指出我正确的方向吗? function delete($id = null) { $ok = $this->Bookmark->delete($id); if($this->RequestHandler->isAjax()) { $this->autoRender = false; $this->autoLayout = false; $response = array(‘status’ => 0, ‘message’ => ‘Could not delete bookmark’); if($ok) { $response = array(‘status’ => 1, ‘message’ => ‘Bookmark deleted’); } $this->header(‘Content-Type: application/json’); echo json_encode($response); exit(); } // Request isn’t AJAX, redirect. $this->redirect(array(‘action’ => […]

在CakePHP表单中禁用使用安全组件和jQuery的输入元素

我在CakePHP中有一个表单,它有两个实时搜索文本输入。 当用户选择结果时,它们中的每一个都更新隐藏字段的值。 该模型称为Record ,涉及的属性是 budget_id program_id concept_id 我用这种方式使用FormHelper创建了一个表单: … Form->create(‘Record’) ?> Create a record Form->hidden(‘Record.budget_id’, array(‘value’ => $budget[‘Budget’][‘id’])) ?> Form->hidden(‘Record.program_id’) ?> Form->input(‘Record.program_id_search’, array(…)) ?> Form->hidden(‘Record.concept_id’) ?> Form->input(‘Record.concept_id_search’, array(…)) ?> Form->submit(‘Send’) ?> Form->end(); ?> … 如您所见,隐藏了存储模型属性的输入字段。 实时搜索框配置了jQuery的自动完成插件。 按照CakePHP手册的建议,我在beforeFilter方法中禁用了两个额外的字段,以便安全组件忽略它们并且表单通过validation: public function beforeFilter() { $this->Security->disabledFields = array( ‘Record.program_id_search’, ‘Record.concept_id_search’, ); } 每当我从Javascript更改隐藏输入的值时,CakePHP似乎都会生气,它会将我发送到blackhole方法。 根据文档,这没关系。 但令我惊讶的是,安全组件一直忽略我的disabledFields设置。 我一直在搜索几个网络资源,每个人都指向disabledFields选项。 但它对我不起作用。 有什么建议? 谢谢!! […]

CakePHP ajaxpost不断返回400 Bad Request

我想在动作中使用ajaxpost。 GET请求工作正常但是当我尝试POST时,我在firebug中看到’400 Bad Request’并且视图返回’Black hole’响应。 这是Jquery请求: $.ajax({ url:”/usermgmt/users/editUser”, type:”POST”, success:function(data) { alert(‘Wow this actually worked’); //ko.applyBindings(self); }, error:function() { alert(‘This will never work’); } }); 这是由于Cake的安全设置还是我在这里缺少的?

validation可编辑字段

我正在使用Jeditable (发布到CakePHP)在我的页面上输入。 我希望用户只在Jeditable字段中填写数字,所以我的想法是也使用jQueryvalidation插件来validation是否只使用了数字,我已经在我的网站的其他部分使用它。 当您单击div时,Jeditable会动态创建带有输入的表单,因此似乎没有任何内容可供Jqueryvalidation绑定到它并且它似乎不正常工作。 我可以通过Jeditable设置表单的类名(它只有一个类atrribute),创建的输入只有一个name属性,默认为“name”。 我的问题: 这是一个只需要数字的好方法吗? 如果是这样,如何使它与Jqueryvalidation 如果没有,那么什么是更好的解决方案? 在此先感谢,我期待着我得到的任何答案;)

如何使用javascript访问模型方法

请看下面的CakePHP代码 Flip2.php (型号) <?php class Flip2 extends AppModel { var $name = 'Flip2'; public $useTable = false; //Increment the correct_answer field of the specific user public function correctAnswer($userID=89, $word) { $setQuery = "UPDATE `users_words` SET `correctanswer` = `correctanswer`+1 WHERE `userid`=$userID && `wordid`='$word' "; query($setQuery); } } Flip2Controller.php (控制器) index.ctp (查看) Html->css(array(‘bootstrap’, ‘mark’, ‘style’)); echo $this->Html->script(array(‘timer’,’swfobject’,’bootstrap.min.js’)); […]

CakePHP AJAX Json Parse使用Model时出错

我正在使用CakePHP 2.5.3.0开发一个应用程序,然后我偶然发现了AJAX的一个问题: 我正在使用从jQuery到CakePHP的AJAX请求来发送用户的登录名和密码,然后CakePHP应该返回validation的JSON响应。 问题是:每当我在Controller中的action方法中使用Model方法时,JSON响应在JSON开始之前就会出现意外的字符。 以下是在Google Chrome上查看回复时的屏幕截图: http : //i.imgur.com/m5x6X4G.png jQuery AJAX请求代码在这里: $.ajax({ url: “/login/signin.json”, cache: false, type: “POST”, dataType: “json”, data: { email: $(“#login-form”).find(“input[name=email]”).val(), password: $(“#login-form”).find(“input[name=password]”).val() }, success: function(response) { self.callback.login(response); } }); 这是LoginController的“signin”方法: public function signin() { if(!$this->request->is(“ajax”)) throw new BadRequestException(); $this->layout = ‘ajax’; $this->response->disableCache(); $this->RequestHandler->respondAs(“application/json”); if($this->request->is(“post”)): $account = $this->Account->validateAccount($this->request->data[’email’], Security::hash($this->request->data[‘password’],”sha1″,true)); if(count($account)>0): $account = […]

用json传递jquery变量

我试图将一个jquery字符串传递给我的cakephp控制器,但我一直收到错误,说json_decode需要一个字符串,但是传递了一个数组。 这是我的ajax的代码: var str = ‘Data that i need’; var jsonString = JSON.stringify(str); $.ajax({ type: “POST”, url: “http:/Configs/”, cache: false, datatype: ‘json’, data: {data : jsonString}, timeout: 100000, success: function(data) { alert(‘Ok’); } }); 并在我的控制器中我试图收到这样的数据: $value = json_decode($_POST[‘data’]); 但我得到上面所述的错误任何想法如何解决这将是非常感谢。