Extjs4 / Spring MVC和response.responseText

时间:2014-02-12 14:19:26

标签: json spring extjs extjs4

在extjs4控制器中我打电话

 form.submit({
                url: '/bookstore/api/books/add',
                waitMsg: 'Uploading your file...',
                success: this.onAddBookSuccess,
                failure: function(response, opts){
                   var  data = Ext.decode(response.responseText);
                    Ext.MessageBox.alert('Error', 'Some problem occurred' + data);
                }
            });

我的'/ bookstore / api / books / add'的春天mvc控制器代码

@RequestMapping(value = "/api/books/add", method = RequestMethod.POST)
public
@ResponseBody
Map<String, ? extends Object> addUser(BookUploadBean bean, BindingResult result) {

Map<String, Object> data = new HashMap<String, Object>();

if (bean.getTitle().compareTo("1") == 0) {
    data.put("success", Boolean.FALSE);
    data.put("errors", "asdasdasd");
    return data;
}

Book book = new Book();
book.setTitle(bean.getTitle());
book.setAuthor(bean.getAuthor());
book.setYear(bean.getYear());
book.setPrice(bean.getPrice());
book.setDescription(bean.getDescription());
book = userRepository.save(book);

data.put("success", Boolean.TRUE);
return data;
}

我从'/ bookstore / api / books / add'获得了2个json响应变体:

  1. 错误json:{“errors”:“asdasdasd”,“success”:false}
  2. 成功json {“成功”:真实}
    服务器确实返回了这个值
    enter image description here
  3. 但是,如果我尝试获取response.responseText,我会得到未定义的值。我如何在失败函数中获得“错误”值?

1 个答案:

答案 0 :(得分:1)

当您提交表单时,您仍然会将响应解码为回调的第二个参数的属性“result”。

所以在你的例子中:

form.submit({
    url: '/bookstore/api/books/add',
    waitMsg: 'Uploading your file...',
    success: this.onAddBookSuccess,
    failure: function(form, action){
        Ext.MessageBox.alert('Error', 'Some problem occurred' + action.result.errors);
    }
});

希望这有帮助:)