1

我有 ExtJS 4 MVC 架构风格的登录表单。也就是说,我有一个控制器和一个视图。

视图文件(仅显示组件创建部分):

initComponent: function() {
    this.items = [
        {border:false, baseCls: 'x-plain', flex:8}, // 13/8 - is golden ratio (approx.)
        {
            border: false,
            baseCls: 'x-plain',
            layout: {
                type: 'vbox',
                pack: 'center',
                align: 'center'
            },
            items: [
                {
                    xtype: 'form',
                    url: 'http://localhost/my_site/login.php',
                    title: 'login_form_title',
                    bodyPadding: 5,
                    width: 280,

                    fieldDefaults: {
                        labelAlign: 'right',
                        labelWidth: 90,
                        anchor: '100%'
                    },

                    items: [
                        {
                            xtype: 'textfield',
                            name: 'username',
                            fieldLabel: 'username',
                            allowBlank: false
                        },
                        {
                            xtype: 'textfield',
                            inputType: 'password',
                            name: 'password',
                            fieldLabel: 'password',
                            allowBlank: false
                        },
                        {
                            border: false,
                            baseCls: 'x-plain',
                            layout: {
                                type: 'hbox',
                                pack: 'center',
                                align: 'center'
                            },
                            items: [
                                {
                                    xtype: 'button',
                                    formBind: true,
                                    disabled: true,
                                    text: 'login',
                                    action: 'login'
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {border:false, baseCls: 'x-plain', flex:13} // 13/8 - is golden ratio (approx.)
    ];
    this.callParent(arguments);
}

在我的控制器中,我将登录按钮 onclick 绑定到某个函数,在该函数中,我使用这段代码将表单提交到服务器:

oForm.submit({
    success: function(form, action) {
        console.log('success');
    },

    failure: function(form, action) {
        console.log(action.failureType);
    }
});

这是http://localhost/my_site/login.php

<?php
exit;

当我按下登录按钮时,我success在开发者控制台中看到。问题如下:

  1. 是否可以将空响应视为failure,即在这种情况下使开发人员控制台记录一些failureType
  2. 为什么空响应被认为是success?非常欢迎提供官方文档或其他资源的链接。

使用以下内容http://localhost/my_site/login.php时,我进入server了开发者控制台,这是正确的。

<?php
echo json_encode(
    array(
        'success'=>false, 
        'errors' => array(
            'reason' => 'Invalid username or password'
        )
    )
);
exit;
4

2 回答 2

2

All you have to do is, to set the HTTP header for the response! Look here or here

Basically you can say, if the status code of the response is not "200 Success" than ExtJS will call the failure method!

In your case you should do following:

<?php
header('WWW-Authenticate: Basic realm="Top Secret Files"');
header("HTTP/1.0 401 Unauthorized");
?>

Also you should set the correct content type. Default is "text/plain" but in you case you should set it to "application/json". If you don't set it to application/json you have to parse the response manually, otherwise ExtJS will do it for you.

header('Content-type: application/pdf');

Here you find a very good tutorial about HTTP headers! And here you have an example in PHP!

I use C# on the server-side, so I don't know the actual php-code, but as long as the HTTP header is 200 OK Ext-JS will call the success-method!

于 2013-01-11T07:28:54.383 回答
0
  1. 是否可以将空响应视为failure,即在这种情况下使开发人员控制台记录一些 failureType
  2. 为什么空响应被认为是success?非常欢迎提供官方文档或其他资源的链接。

Ext.form.submit()根据以下响应被标记为失败 -

{
    success: false,
    errors: {
        clientCode: "Client not found",
        portOfLoading: "This field must not be null"
    }
}

空响应只要不包含success:false

您可以在此处了解更多信息

于 2013-01-10T19:47:14.333 回答