1. #1
    Sencha User
    Join Date
    Nov 2012
    Posts
    2
    Vote Rating
    0
    Answers
    1
    Jin_NYC is on a distinguished road

      0  

    Default Answered: Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String:

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


    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:

    Code:
        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:

    Code:
    <%@ 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.

  2. 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:

    Code:
    {
        success: <%=successFlag%>,
        "invalid" : [{"name":"2222"},{"name":"3333"}]
    }
    to the server side code. Hopefully someone else find it help...

  3. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    17,169
    Vote Rating
    674
    Answers
    474
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    A file upload expects you to return a json response the same way you would for a normal form submit.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  4. #3
    Sencha User
    Join Date
    Nov 2012
    Posts
    2
    Vote Rating
    0
    Answers
    1
    Jin_NYC is on a distinguished road

      0  

    Default


    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:

    Code:
    {
        success: <%=successFlag%>,
        "invalid" : [{"name":"2222"},{"name":"3333"}]
    }
    to the server side code. Hopefully someone else find it help...

Thread Participants: 1

Tags for this Thread