将数据(POST)从jquery发送到cakephp中的控制器

时间:2014-06-13 15:32:42

标签: javascript php jquery ajax cakephp

那么,

问题是我总是得到同样的错误“匿名函数”是因为当我想通过post发送数据这些从未找到Controller或Controller的路径时,我已经证明有三种方法可以在JS。

validate.js

$(document).ready(function(){
                var route ="<?php echo Router::Url(array('controller' => 'soyaproductorcompras','action' => 'validate_form', 0 => $this->request->params['pass'][0], 1 => $this->request->params['pass'][1]));?>";
    $('#proveedor_carnet').blur(function(){
        $.post(route,
        { 
              field: $('#proveedor_carnet').attr('id'), 
              value: $('#proveedor_carnet').val() 
            },
            handleNmeValidation
        );
    });
    function handleNmeValidation(error){
        if(error.length > 0){
            if($('#proveedor_carnet-notempty').length === 0){
                $('#proveedor_carnet').after('<div id="proveedor_carnet-notEmpty" class="error-message">' + error + '</div>');
            }
        }else{
            $('#proveedor_carnet-notEmpty').remove();
        }
    }
});

这里有我正在使用的代码,我的验证问题就是路径。

第一个选项:

var route ='/cake/soyaproductorcompras/validate_form';

第二个选项

var route ='http://localhost:8080/cake/soyaproductorcompras/validate_form';

和最后一次

var route ="<?php echo Router::Url(array('controller' => 'soyaproductorcompras','action' => 'validate_form', 0 => $this->request->params['pass'][0], 1 => $this->request->params['pass'][1]));?>";

我看到类似的问题,但不适用,例如this

这是控制器验证表单:

public function validate_form()
{    
    $this->loadModel('SoyaProductorCompra');
    $this->loadModel('SoyaProveedor');
    $this->autoRender = FALSE;
    if ($this->request->is('ajax')) {
        $proveedor=$this->data['SoyaProductorCompra'][$this->params['form']['field']]=$this->params['form']['value'];
        $existe = $this->SoyaProveedor->find(
            'first',
            array(
                'fields' => array(
                    'SoyaProveedor.id'
                ),
                'conditions' => array(
                    'SoyaProveedor.id' => $proveedor
                )
            )
        );
         if(empty($existe)){
            //return false; 
             echo "no existe el usuario";
        }else{
            //return true; 
            echo "el usuario existe";
        }
    }           
}

错误完成

POST http://localhost:8080/cake/soyaproductorcompras/%3C?php%20echo%20Router::Ur…pass%27][0],%201%20=%3E%20$this-%3Erequest-%3Eparams[%27pass%27][1]));?%3E 404 (Not Found) jquery.js:9666
jQuery.ajaxTransport.send jquery.js:9666
jQuery.extend.ajax jquery.js:9211
jQuery.each.jQuery.(anonymous function) jquery.js:9357
(anonymous function) VM811 validation.js:4
jQuery.event.dispatch jquery.js:4624
jQuery.event.add.elemData.handle

0 个答案:

没有答案