Extjs4 - 表单提交总是 returns 失败

Extjs4 - Form submit always returns a failure

我正在使用 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 return

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

您需要在响应中附加参数 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

不要发送 null Response ...但预期的 JSON 以及适当的 content-type header:

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

在我的例子中(Spring 使用 Ext JS 6.2 启动应用程序),如果要返回对象列表,则可以使用包装器。例如,

而不是做:

@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);
}

其中用户 class 是:

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;
   }
}