未捕获的TypeError:无法读取属性' responseText'未定义的

时间:2012-11-28 19:40:32

标签: extjs extjs4

buttons: [
    {
        text: "Add User",
        id: "new-record-add-button",
        handler: function() {
            var form = this.up('form').getForm();
            form.submit({
                url: BasePath+'/main/admin/adduser',
                method: 'POST',
                waitTitle: 'Authenticating',
                waitMsg: 'Please Wait',
                success: function(form, action) {
                win.close()
                     Ext.Msg.show({
                         title:'Success'
                        ,msg:'User added successfully'
                        ,modal:true
                        ,icon:Ext.Msg.INFO
                        ,buttons:Ext.Msg.OK
                });
                },

            failure: function(form, action) {

                console.log(action.response.responseText);
                obj = Ext.JSON.decode(action.response.responseText);
                console.log(obj);
                Ext.Msg.alert('Error',obj.errors)
                form.reset();
            }
        })

                //this.up("window").close();
        }

    },
    {
        text: "Cancel",
        handler: function() {
            this.up("window").close();
        }
    }
]

当我到达表单中的失败函数时,我收到以下错误:

Uncaught TypeError: Cannot read property 'responseText' of undefined 

这是我的PHP代码:

public function adduserAction()
{
            $response = new JsonModel();
            //$error = array();
            $errors="";
            if(!ctype_alpha($_POST["first_name"])) {
                $errors.="First Name cannot contain characters and numbers";
            }
            if(!ctype_alpha($_POST["last_name"])) {
                $errors.="Last Name cannot contain characters and numbers";
            }

            if(!filter_var($_POST['email_address'], FILTER_VALIDATE_EMAIL)) {
                $errors.="Email should be of the format john.doe@example.com";
            }
            if(empty($_POST["role"])) {
                $errors.="Role cannot be empty";
            }
            if($errors!="") {
                $response->setVariables(array("success"=>false, "errors"=>$errors));

            }
            else {
                $response->setVariables(array("success"=>true, "errors"=>$errors));

            }
            return $response;
}

2 个答案:

答案 0 :(得分:0)

responseText是ExtJs的东西 - 它表示在解码之前从服务器返回的实际文本(例如,使用echo)。

您应该在operationrequest个对象中的异步回调中获取它,除非存在某种服务器异常,或者将success设置为false,这就是为什么你没有在故障处理程序中得到它。

为了真正理解它的作用,我建议您查看Connection.js

答案 1 :(得分:0)

如果您通过ExtJs提交表单,那么在表单提交成功时,需要将response.responseText设置为{“sucess”:“true”}。如果您要将表单提交到某个页面,则必须确保从后端返回此对象。否则你必须覆盖现有的onSuccess方法。

第二种方式是这样的,


Ext.override(Ext.form.action.Submit,{

    onSuccess: function(response) {
        var form = this.form,
            success = true,
            result = response;
            response.responseText = '{"success": true}';
            form.afterAction(this, success);
        }
    });

将此代码段放在您的应用程序中,看看魔术。干杯。 :)