extjs 提交与 AJAX .request的比较

8zzbczxx  于 2022-11-23  发布在  其他
关注(0)|答案(3)|浏览(201)

我有一个Ext.form.Panel,其中包含多个文本区域和文件输入,如下所示

// https://requestbin.com/r/en0ej96odon2sm/1n6r1tb49KK6eObGMPHlYa1hh4C

Ext.create({
    xtype: 'formpanel',
    renderTo: document.body,
    buttons: {
        submit: 'onSubmit',
    },

    controller: {

        onSubmit: function () {
            var form = this.getView();

            form.submit({
                method: 'POST',
                url: 'https://en0ej96odon2sm.x.pipedream.net/test1',
                success: function () {}
            });
        },

        onSubmitTest: function () {
            var form = this.getView();
            Ext.Ajax.request({
                url: 'https://en0ej96odon2sm.x.pipedream.net/test2',
                method: 'POST',
                params: {
                    data: form.getValues(),
                },
                success: function () {}
            });
        },

    },

    items: [{
        xtype: 'textareafield',
        name: 'testfield',
        label: 'testfield',
        value: 'test\nasd',
    }, {
        xtype: 'filefield',
        label: 'Upload Test',
        name: 'basedata-test',
    }, {
        xtype: 'button',
        text: 'Ajax.request(), linebreaks but no files',
        handler: 'onSubmitTest',
    }]
});

发布结果:https://requestbin.com/r/en0ej96odon2sm/1n6mtu8QtyreaisCAmV3csO724Q
小提琴:https://fiddle.sencha.com/#view/editor&fiddle/3b9j
所以,因为我需要fileinput/multipart,所以我必须使用form.submit({})。
但是当我这样做的时候,我在$_POST Var中的Server端没有得到换行符。
当我执行 AJAX .request({})时,一切看起来都很好,但是缺少$_FILES,所以这实际上不是一个选项。(但是这是有文档记录的)。
我还尝试将jsonSubmit添加到表单中(然后我根本没有得到$_POST)。false我得到了换行符,但提交后表单消失了(我不知道为什么)。
有解决方案吗?还是我做错了什么?

eyh26e7m

eyh26e7m1#

你可以使用下面的重写。希望它不会使框架不稳定;)

// https://requestbin.com/r/en0ej96odon2sm/1n6r1tb49KK6eObGMPHlYa1hh4C

// Override
Ext.define('overrides.form.Panel', {
    override: 'Ext.form.Panel',
    privates: {
        createSubmissionForm: function (form, values) {
            var fields = this.getFields(),
                name, input, field, fileTrigger, inputDom;

            if (form.nodeType === 1) {
                form = form.cloneNode(false);

                for (name in values) {
                    input = document.createElement('textarea');
                    input.setAttribute('type', 'string');
                    input.setAttribute('name', name);
                    input.innerHTML = values[name];
                    form.appendChild(input);
                }
            }

            for (name in fields) {
                if (fields.hasOwnProperty(name)) {
                    field = fields[name];

                    if (field.isFile) {
                        // The <input type="file"> of a FileField is its "file" trigger button.
                        fileTrigger = field.getTriggers().file;
                        inputDom = fileTrigger && fileTrigger.getComponent().buttonElement.dom;

                        if (inputDom) {
                            if (!form.$fileswap) {
                                form.$fileswap = [];
                            }

                            input = inputDom.cloneNode(true);
                            inputDom.parentNode.insertBefore(input, inputDom.nextSibling);
                            form.appendChild(inputDom);
                            form.$fileswap.push({
                                original: inputDom,
                                placeholder: input
                            });
                        }
                    } else if (field.isPassword) {
                        if (field.getInputType() !== 'password') {
                            field.setRevealed(false);
                        }
                    }
                }
            }

            return form;
        }
    }
});

Ext.create({
    xtype: 'formpanel',
    renderTo: document.body,
    buttons: {
        submit: 'onSubmit',
    },

    controller: {

        onSubmit: function () {
            var form = this.getView();

            form.submit({
                method: 'POST',
                url: 'https://en0ej96odon2sm.x.pipedream.net/test1',
                success: function () {}
            });
        },

        onSubmitTest: function () {
            var form = this.getView();
            Ext.Ajax.request({
                url: 'https://en0ej96odon2sm.x.pipedream.net/test2',
                method: 'POST',
                params: {
                    data: form.getValues(),
                },
                success: function () {}
            });
        },

    },

    items: [{
        xtype: 'textareafield',
        name: 'testfield',
        label: 'testfield',
        value: 'test\nasd',
    }, {
        xtype: 'filefield',
        label: 'Upload Test',
        name: 'basedata-test',
    }, {
        xtype: 'button',
        text: 'Ajax.request(), linebreaks but no files',
        handler: 'onSubmitTest',
    }]
});
t98cgbkg

t98cgbkg2#

不理想,但你也可以这样做:

form.submit({
            method: 'POST',

            //just like the ajax
            params: {
                data: form.getValues(),
            },
            url: 'https://en0ej96odon2sm.x.pipedream.net/test1',
            success: function () {}
        });
5kgi1eie

5kgi1eie3#

下面是使用 AJAX .request而不是form.submit的简单解决方法
我需要这样做是因为我必须设置Authorization头,而这在框架使用的IFRAME中是无法实现的
因此,防止Ext.data.request. AJAX 设置Content-Type头似乎可以完成这项工作。
将自动设置multipart/form-data
警告:options.headersdefaultHeaders都不应该已经有'Content-Type'信头

Ext.define('Override.data.request.Ajax', {
  override: 'Ext.data.request.Ajax',
  setupHeaders: function(xhr, options, data, params) {
    if (data instanceof FormData) {
      if (Ext.apply({}, options.headers || {}, this.defaultHeaders).hasOwnProperty('Content-Type')) {
        console.warn('The Content-Type header must not be set before request if you need to use FormData with this override');
      }
      /* prevent Ext.data.request.Ajax from setting Content-Type header */
      return this.callParent([xhr, options, null, null]);
    } else {
      return this.callParent(arguments);
    }
  }
});

并调用 AJAX .request,其中FormData为rawData

var formData = new FormData();
var files = myView.down('filefield').getFiles();
if (files.length > 0) {
  formData.append('file', files[0], files[0].name);
}
Ext.Ajax.request({
  url: 'your_url',
  rawData: formData,
  success: function(response) {
    // handle success
  },
  failure: function(response) {
    // handle failure
  }
});

相关问题