Extjs 4-表单提交始终返回失败

smtd7mpg  于 2022-11-04  发布在  其他
关注(0)|答案(3)|浏览(109)

我正在使用ExtJS 4表单提交一个excel文件,但即使请求成功,它也注册了一个失败。form.submit函数期望看到什么?

表格

xtype: 'form',
name: 'upload_form',
items: [{
      text: 'File Upload',
      xtype: 'fileuploadfield',
      name: 'upload_btn',
      buttonOnly: true,
      hideLabel: true,
      allowBlank: false,
      clearOnSubmit: false
}]

控制器

'filter fileuploadfield[name="upload_btn"]': {
            change: this.UploadClick
        }
    ...
    UploadClick: function (vb, s) {
    var controller = this,
        form = controller.getUploadForm();

    if (form.isValid()) {
        form.submit({
            url: '/upload',
            waitMsg: 'Uploading your csv...',
            success: function (fp, o) {
                Ext.Msg.show({
                    title: 'Upload Complete',
                    msg: o.response.responseText,
                    icon: 'save-success',
                    buttons: Ext.Msg.OK
                });
            },
            failure: function (fp, o) {
                Ext.Msg.show({
                    title: 'Upload Error',
                    msg: o.response.responseText,
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            }
        });
    }
}

Java返回

Response.ResponseBuilder builder;
...
builder = Response.ok(null);
        return builder.build();
xdnvmnnf

xdnvmnnf1#

不要发送nullResponse ...而是发送预期的JSON沿着正确的内容类型头:

String json = "{\"success\": true}";
return Response.ok(json, MediaType.APPLICATION_JSON).build();
rlcwz9us

rlcwz9us2#

您需要附加参数success作为响应:
例如:

{
    "success":true, // note this is Boolean, not string
    "msg":"File uploaded"
}

如文档中所示:https://docs.sencha.com/extjs/4.2.6/#!/api/Ext.form.Basic-method-submit

c3frrgcw

c3frrgcw3#

在我的例子中(使用ExtJS6.2的 Boot 应用程序),如果要返回一个对象列表,可以使用 Package 器。
而不是:

@GetMapping
public ResponseEntity<List<User>> getUsers() {
    List<User> users = repository.getAll();
    return new ResponseEntity<>(users, HttpStatus.OK);
}

请执行以下操作:

@GetMapping
public ResponseEntity<Users> getUsers()
{
    List<User> users = repository.getAll();
    return new ResponseEntity<>(new Users(users, true), HttpStatus.OK);
}

其中Users类为:

public class Users
{
   private List<User> users;
   private boolean success = true;
   public Users(List<User> users, boolean success)
   {
      this.users = users;
      this.success = success;
   }

   public List<User> getUser()
   {
      return users;
   }

   public void setUser(List<User> users)
   {
      this.users = users;
   }

   public boolean isSuccess()
   {
      return success;
   }

   public void setSuccess(boolean success)
   {
      this.success = success;
   }
}

相关问题