关于ExtJs4提交数据到jsp页面,返回正确的json显示例子。及error:You're trying to decode an invalid JSON String...解决方法

json提交信息到jsp页面中处理,返回json数据到前台显示例子:

main.js

Ext.onReady(function(){
	var formPanel = Ext.create('Ext.form.Panel', {
	    title: '输入项',
	    renderTo: Ext.getBody(),
	    //bobyPadding : 10,
	    items: [{
	        xtype: 'textfield',
	        name: 'name',
	        fieldLabel: '功能名称',
	        border:'20',
	        allowBlank: false  // 表单项非空
	    },{
	    	xtype:'button',
	    	text:'保存',
	    	handler:function(){
	    		var basicForm = formPanel.getForm();//获取basicForm对象  
                if(basicForm.isValid()){
                	 basicForm.submit({ 
                		 url : 'insertInfo.jsp',  
                         method : 'post',  
                         waitTitle : "提示",                 //等待的标题  
                         waitMsg : '正在提交数据...',           //等待的信息  
                         //params : {id: 'tom',foo: 'bar'},//传递参数  
                         success:function(form,action){
                        	 Ext.Msg.alert('Success', action.result.info);
                         },
                         failure : function(form,action){  
                         }
                	 })
                }//如果客户端的验证通过则返回真  
	    	}
	    }]
	});
	
	
	Ext.create('Ext.window.Window',{
		titile:'窗体',
	    width: 400,
	    modal: true,
	    items: [formPanel   //填写表单面板
	            ],
		renderTo:Ext.getBody()
	}).show();
});

insertInfo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
  <%
	String name = (String)request.getParameter("name");  //获取前台表单传递的参数
	System.out.println("name:"+name);
	String text = "{success:true,info:'"+name+"'}";  //拼写json
	response.getWriter().write(text);     //回传到前台
  %>
注意,其中上面的jsp文件不能有任何html代码,否则传递回去的json无法解析,会有报错信息 error:You're trying to decode an invalid JSON String...


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值