【问题标题】:cakePHP- Ajax post request- sending data back to callback after successcakePHP-Ajax 发布请求-成功后将数据发送回回调
【发布时间】:2014-02-18 19:38:20
【问题描述】:

好的,所以我将以下 ajax 发布请求包装在一个模糊事件中,如下所示:

$('#name, #surname, #email').blur(function(e){        
        $.post(
            '/validate_form', // it will submit to the validate_form action
            {field: $(this).attr('id'), value: $(this).val()}, 
            handleValidation
        );

    });

在我的handleValidation 回调中,我想取回触发模糊事件的元素的ID(即field)。因此,我想到的方法是在 ajax 发布请求成功后将其传递回回调,因为发布请求已发送。但是我不完全确定如何做到这一点。我已经在我的验证回复中收到一条错误消息,但这是请求的通常自动回复。

function handleValidation(error, {//i want to get another variable sent here..}) {
        if (error.length > 0) {
            if ($('{placeholder for field id}-notEmpty').length == 0) {
                $('#{placeholder for field id').after('<div id="{placeholder for field id-notEmpty" class="error-message">' + error + '</div>');
            }
        }else{
            $('#{placeholder for field id-notEmpty').remove();
        }
    } 

public function validate_form(){            
        if($this->RequestHandler->isAjax()){
        $this->request->data['Model'][$this->request->data['field']] = $this->request->data['value'];
        $this->Donor->set($this->request->data);
             if($this->Model->validates()){
               $this->autoRender = FALSE;
             }else{
                //somewhere here, i need to pass in $this->request->data['field'] back to callback function handleValidation.
             }

        }
}

我该怎么做?谢谢

【问题讨论】:

  • 切换到使用 $.ajax 并将元素作为上下文参数传递。或者,将您的 handleValidation 函数替换为闭包。
  • 你的意思是匿名函数吗? .. 如function(error, my_var) ?
  • 我的第二个示例将显示关闭方式。它是一个使用附加参数执行验证函数的闭包(在这种情况下,闭包是匿名的,但不是必须的)

标签: javascript php jquery ajax cakephp


【解决方案1】:

有几种方法可以做到这一点,所有这些都围绕着访问this。您可以将其作为参数传递给回调,将其作为上下文传递给回调,或者将回调设为闭包。

$.ajax('/validate_form',{
    data: {
        field: $(this).attr('id'),
        value: $(this).val()
    }
    context: this,
    success: handleValidation
});

function handleValidation() {
    console.log(this); // the element that you acted on
}

var self = this;
$.post(
    '/validate_form', // it will submit to the validate_form action
    {field: $(this).attr('id'), value: $(this).val()}, 
    function (data) {
        handleValidation(data,self);
    }
);
function handleValidation(data,el) {
    console.log(el); // the element that you acted on
}

【讨论】:

  • 您好,感谢您的回复!使用第一种方法,我也可以像第二种方法一样获得错误参数吗?哪一个最好?我可以看到 jquery ajax 比 post 更灵活,这就是为什么我询问错误参数.. 它也会被传递吗?非常感谢!
  • 您无法使用任何一种方法获取错误参数,即使使用您的原始代码也是如此。错误通过一个完全不同的回调。也许您的意思是响应数据?
  • 在这种情况下,是的,这就是我正在使用的data 参数。
【解决方案2】:

闭包对于在声明时捕获变量的状态很有用,因此它们可以在以后使用。要使用匿名函数将回调转换为闭包,请执行以下操作

$('#name, #surname, #email').blur(function(e){        
    var elem = $(this).attr('id');
    $.post(
        '/validate_form', // it will submit to the validate_form action
        {field: $(this).attr('id'), value: $(this).val()}, 
        function (error, elem) { handleValidation(error, elem) }
    );
});

如果对你更有意义的话,你也可以在没有匿名函数的情况下这样做

$('#name, #surname, #email').blur(function(e){       
    var elemNow = $(this).attr('id');
    var handleValidation; //declare outside the closure
    function closure(error, elem) {
        handleValidation = function(error){
            //has access to elem's value at the time of closure's declaration
            console.log(elem);
        }
    }(error, elemNow); //run closure() now 
    $.post(
        '/validate_form', // it will submit to the validate_form action
        {field: $(this).attr('id'), value: $(this).val()}, 
        handleValidation }
    );
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    相关资源
    最近更新 更多