Search code examples
jsonspringextjsextjs4

Extjs4 / Spring MVC and response.responseText


In extjs4 controller i call

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

My spring mvc controller code for '/bookstore/api/books/add'

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

I get from '/bookstore/api/books/add' 2 variants of json response:

  1. Error json : {"errors":"asdasdasd","success":false}
  2. Success json {"success":true}
    And server really returned this values
    enter image description here

But if i try get response.responseText, i get undefined value. How I can get "errors" values in my failure function?


Solution

  • When u submit a form you get the response still decoded into the property "result" of the second parameter of the callback.

    So in your example:

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

    Hope this help :)