cakePHP- Ajax发布请求 - 成功后将数据发送回回调

时间:2014-02-18 19:38:20

标签: javascript php jquery ajax cakephp

好的,所以我将以下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 post请求成功后将其传回回调,因为发送了post请求。但是我不完全确定如何做到这一点。我已在响应中收到错误消息以进行验证,但这是请求中通常的自动响应。

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

        }
}

我该怎么做?谢谢

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

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

$('#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 }
    );
});