2012-12-04 46 views
2

我的代码存在问题。 我正在尝试使用ExtJS和Codeigniter进行上传文件格式。错误:Uncaught Ext.JSON.decode():您试图解码无效的JSON字符串

这里是我下面的代码,

Ext.require([ 
    'Ext.form.field.File', 
    'Ext.form.Panel', 
    'Ext.window.MessageBox' 
]); 

Ext.onReady(function(){ 

    Ext.create('Ext.form.Panel', { 
     title: 'Upload a Photo', 
     width: 400, 
     bodyPadding: 10, 
     frame: true, 
     renderTo: Ext.getBody(), 
     items: [{ 
      xtype: 'filefield', 
      name: 'photo', 
      fieldLabel: 'Photo', 
      labelWidth: 50, 
      msgTarget: 'side', 
      allowBlank: false, 
      anchor: '100%', 
      buttonText: 'Select Photo...' 
     }], 

     buttons: [{ 
      text: 'Upload', 
      handler: function() { 
       var form = this.up('form').getForm(); 
       if(form.isValid()){ 
        form.submit({ 
         url: '../upload', 
         waitMsg: 'Uploading your photo...', 
         success: function(fp, o) { 
          Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.'); 
          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'); 
           } 
          }); 
         } 
        }); 
       } 
      } 
     }] 
    }); 

}); 

但像显示我的错误控制台输出“未捕获Ext.JSON.decode():你试图解码的JSON字符串无效”。 因此,如果任何人有同样的问题或正在解决这个问题,请告诉我..

谢谢!

结论: 哦,我看.. 其实我用笨作为我的框架。 如果我返回JSON这个网址这个问题将得到解决..

url: '../upload', 
waitMsg: 'Uploading your photo...', 
success: function(fp, o) { 
Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.'); 
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'); 
    } 
    }); 

所以,我创造我的控制器是这样的(如样品)..

public function upload(){ 
     echo json_encode(1); 
    } 

并没有更多的错误结果我代码.. THANKS Broncha !!

回答

1

您的AJAX响应不是纯粹的JSON。尝试输出response控制台

success: function(response, request) { 
    // do whatever you need to with the generated HTML 
    console.log(response); 
}, 

,并检查响应是纯粹的JSON

0

我的问题是相同的,我搜索在谷歌刚刚知道这个问题 URL Solution here

我认为一个解决方案这是使用Sencha免费和购买的区别。 当使用Form.load(...)获得JSON对象映射到表格的所有文本框,你不仅写JSON字符串亦真亦幻还留着这样的格式:

{success:true,data:{... json string object ...}} 
0

曾有一个类似的问题,但其实我有对服务器的访问代码进行更改。

ExtJS4

Ext.define('MyApp.store.Thing', { 
extend : 'Ext.data.Store', 
model : 'MyApp.model.Thing', 
autoLoad : true, 

proxy : { 
    type : 'jsonp', // Needs to be used because we are going cross domain 
    url : 'http://remotehost:8081/Servlet', 
    reader : { 
     type : 'json', 
     totalProperty : 'totalCount', 
     root : items, 
     successProperty : 'success', 
    }, 
} }); 

的Java Servlet

... 
String cb = request.getParameter("callback"); 
response.setContentType("application/x-javascript; charset=utf-8"); 
usersObject.put("totalCount", usersCount); 
usersObject.put("success", true); 
usersObject.put("items", itemsArray); 
response.getWriter().write(cb + '(' + itemsObject.toString() + ')'); 
... 

这点很关键:

response.getWriter()写(CB + '(' + itemsObject.toString()+ ')');

0

我遇到这个错误的Struts 2

下,我所要做的就是用在struts.xml中的以下内容:

<result name="success" type="stream"> 
    <param name="contentType">application/json</param> 
    <param name="inputName">someBeanAttribute</param> 
</result> 

所以,基本上你需要在应用程序中的某处指定application/json