【问题标题】:EXTJS 4 How to get Json response back on success of submit in form?EXTJS 4 如何在表单提交成功后获得 Json 响应?
【发布时间】:2013-10-13 21:08:56
【问题描述】:

我能够在 firebug 中看到 json 响应,但是当我成功解码表单的 ACTION 时,它给出了 undefined 。

{
    xtype: 'form',
    x: 30,
    y: 520,
    height: 80,
    bodyPadding: 10,
    title: 'myuploadform',
    fileUpload: true,
    standardSubmit: false,
    autoHeight: true,
    bodyStyle: 'padding: 10px 10px 10px 10px;',
    labelWidth: 50,

    items:[{
        xtype: 'fileuploadfield',
        id: 'filedata',
        emptyText: 'Select a document to upload...',
        fieldLabel: 'File',
        buttonText: 'Browse'
    }],

    buttons: [{
        text: 'Upload',
        handler: function() {
            var form = this.up('form').getForm();
            if(form.isValid()){
                alert("submit");

                form.submit({
                    url: 'myurl'
                    waitMsg: 'Uploading file...',

                    success: function (form,action) {
                        var data= Ext.JSON.decode(action.response.responseText);
                        alert("Success: " + data.msg);
                        Ext.Msg.alert('Success', action.result.message);
                    },

                    failure: function (form, action) {
                        var data = Ext.decode(action.response.responseText);
                        alert("Failure: " + data.msg);
                    }
                });
            }
        }
    }]
}

我的 url 返回 json 响应,我想在提交成功时处理它。如果有人试过请告诉我

【问题讨论】:

  • 响应 json 应该包含 'success: true' 以便执行成功处理程序。

标签: extjs


【解决方案1】:

尝试纯 JavaScript 方式

var data= JSON.parse(action.response.responseText);

或 ExtJs 3.x 方式

var data= Ext.util.JSON.decode(action.response.responseText);

ExtJs 4 及以上版本

var data= Ext.JSON.decode(action.response.responseText);

【讨论】:

  • 在 ExtJS 4(至少 4.2.2)中,JSON 已升级,不再是 og util 的一部分,您必须使用:var data = Ext.JSON.decode(action.response.reponseText)
【解决方案2】:
         success : function(response, opts)
            {
                response = Ext.decode(response.responseText);
                if(response.success==true){
                    alert("Authentication sucess");

                }else{

                    Ext.MessageBox.alert('Alert','Invalid User name or password');
                }


            },
    failure:function(response, opts)
              {
                  Ext.MessageBox.alert('Alert','Exception is occured please contact administrator');


              }

【讨论】:

    猜你喜欢
    • 2013-12-27
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多