PDA

View Full Version : Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String:



Jin_NYC
7 Nov 2012, 2:27 PM
Hi guys,

I've been using sencha for a while but never find a solution for this error.

The js code is a simple file upload field and a submit button:



var enter = Ext.create('Ext.form.Panel', {
frame:false,
header:false,
border:false,
url:'myUpload.jsp',
title:'MY FORM',
bodyStyle:'padding:5px 5px 0',
width:900,
fieldDefaults:{
msgTarget:'side'
},
items:[
{
xtype:'panel',
title:'loadExclusionsP',
layout:{
type:'hbox',
align:'center'
},
border:false,
header:false,
items:[
{
xtype:'component',
html:'Load CSV on Excel: ',
name:'text',
width:150
},
{
xtype:'filefield',
name:'exclusionsCSV',
required: true
},
{
xtype:'button',
text:'Load file',
handler:function () {
var formUp = enter.getForm();
if (formUp.isValid()) {
formUp.submit({
success: function(form, action) {
Ext.Msg.alert('Success',action.result.msg);
},
failure: function(form,action) {
Ext.Msg.alert('failed',action.result.msg);
}
});
}else{
Ext.Msg.alert('Failure', 'Not valid form');
}
}
}
]
}
]
});


and the server side code is also straight forward:



<%@ page import="java.io.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<%
response.setContentType("text/html");


String result="";
File file;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;


DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("c:\\temp"));
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);


FileItem f1 = items.get(0);


System.out.println(f1.getString());
%>


Can any one see why the error happens, thank you so much for your time and help.

evant
7 Nov 2012, 3:08 PM
A file upload expects you to return a json response the same way you would for a normal form submit.

Jin_NYC
9 Nov 2012, 1:18 PM
Thank you for your reply, I later find the way write the JSON response.

The way to solve it is add a valid JSON string, for example:



{
success: <%=successFlag%>,
"invalid" : [{"name":"2222"},{"name":"3333"}]
}

to the server side code. Hopefully someone else find it help...