java - 未捕获的分机错误 : You're trying to decode an invalid JSON String: Form Submission using Ext JS and Spring MVC

标签 java spring-mvc extjs4 form-submit json

提交 Ext JS 表单后出现以下错误:

Uncaught Ext.Error: You're trying to decode an invalid JSON String

JS:

Ext.onReady(function() {

        var simple = Ext.create('Ext.form.Panel', {

                    frame : true,
                    title : 'Login Form',
                    bodyStyle : 'padding:5px 5px 0',
                    width : 350,
                    fieldDefaults : {
                        msgTarget : 'side',
                        labelWidth : 75
                    },
                    defaultType : 'textfield',
                    defaults : {
                        anchor : '100%'
                    },

                    items : [{
                                fieldLabel : 'User Name',
                                name : 'userName',
                                allowBlank : false,
                                emptyText : 'UserName'
                            }, {
                                fieldLabel : 'Password',
                                name : 'password',
                                allowBlank : false,
                                inputType : 'password',
                                emptyText : 'Password'
                            }],

                    buttons : [{
                        text : 'Save',
                        handler : function() {
                            var form = this.up('form').getForm();
                            form.submit({
                                        url : saveFormUrl
                                    //  waitMsg : 'Sending the info...',
                                    //  success : function(fp, o) {
                                    //      Ext.Msg.alert('Success',
                                    //              'Form submitted.');
                                    //  }
                                    });
                        }
                    }, {
                        text : 'Cancel'
                    }]
                });
        simple.render(document.body);
        simple.getEl().center();
    });

Controller 类:

@Controller
public class UserController {

private static final Logger logger = LoggerFactory
        .getLogger(TController.class);

private TService tService = null;

@Autowired
public void setTService(TService tService) {
    this.tService = tService;
}

@RequestMapping(value = "/index.html", method = RequestMethod.GET)
public String home() {
    System.out.println("Welcome home!");
    return "login";
}

@RequestMapping(value = "/save-form.html", method = RequestMethod.POST)
public ModelAndView submitData(User user){
    System.out.println("User name:"+user.getUserName());
    ModelAndView mv = new ModelAndView("htmlLinks");
    return mv;
}

save-form.html:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>POC</title>

</head>
<body>
 Welcome User !!
</body>
</html>

我做错了什么?解决办法是什么?我正在使用 Ext JS 4 和 Spring MVC。

最佳答案

根据documentation对于 form.submit,看起来响应需要是 JSON 或 XML,格式如下:

{
    success: true,
    data: {
        url: "http://somewhere",
        someData: "whatever you want"
    }
}

在 JavaScript 的成功处理程序中,您可以引用 o.data.[variable] 来获取自定义数据。

不幸的是,您将需要更改 submitData 方法(在您的 Controller 中)以返回上面定义的结构中的 JSON 响应对象。在响应对象中,您可以包含指向 save-form.html 的 URL。然后您可以在成功处理程序中为它发出额外的 GET 请求。

我不知道这是否可行,因为我没有使用 Ext JS 的经验,但我希望成功处理程序看起来像这样:

success: function(fp, o) {
    Ext.Msg.alert('Success', 'Form submitted.');
    Ext.Ajax.request({
        url: o.data.url,
        method: "GET",
        success: function(response, request) {
            // do whatever you need to with the generated HTML
            alert(response.responseText);
        },
        failure: function(response, request) {
            alert('failed');
        }
    });
}

关于java - 未捕获的分机错误 : You're trying to decode an invalid JSON String: Form Submission using Ext JS and Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11635396/

相关文章:

java - Java 中 Final 字段和 volatile 字段之间内存模型语义的差异

spring-mvc - 带有 XML 模板的 Spring Boot 和 Thymeleaf

javascript - ExtJS 5.0 - 覆盖以在网格列上提供自定义排序

google-chrome - Chrome 中的 ExtJS 4 图表轴显示问题

java - 我们可以将字符串插入到哈希表对象中吗

java - 获取所选元素内的所有元素作为 Element Jsoup android 的数组

java - 单击时更改所有按钮的背景

java - grails 中面向方面的编程问题。切入点方法未执行

java - spring validator 中如何获取请求参数?

javascript - 删除 ExtJs 4 默认类