PDA

View Full Version : FileUpload Response Problem



lukep1984
9 Jan 2012, 7:13 AM
Hello!!
I am using ext 4.0.7 with struts2.
I am trying to figure out what is the correct content type and format of response after file uploading using Form Panel with Ext.form.field.File. Because when I return empty json I get the following warning and error:

Resource interpreted as Other but transferred with MIME type undefined.

Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">null</pre>


And of course JSON returned from action is just '{}'.
I also tried to change the content type of response to 'text/html but it doesn't help.

Any help would be appreciated.

Best Regards,
Luke

mitchellsimoens
9 Jan 2012, 9:21 AM
You should try to return { "success" : true }

lukep1984
9 Jan 2012, 11:38 PM
I tried and I have got the exactly same error:


Resource interpreted as Other but transferred with MIME type undefined.

Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">{"success":true}</pre> ext-all-debug.js:5001

Ext.Error.Ext.extend.statics.raise ext-all-debug.js:5001
(anonymous function) ext-all-debug.js:5165
Ext.define.handleResponse ext-all-debug.js:63937
Ext.define.processResponse ext-all-debug.js:63730
Ext.define.onSuccess ext-all-debug.js:63904
Ext.apply.callback ext-all-debug.js:5283
Ext.define.onUploadComplete ext-all-debug.js:17937
(anonymous function) ext-all-debug.js:1537
(anonymous function)
wrap ext-all-debug.js:11088



Best Regards,
Luke

mitchellsimoens
10 Jan 2012, 5:12 AM
Looks like in your response, the JSON is wrapped by <pre> tags which is causing the invalid JSON state.

lukep1984
10 Jan 2012, 7:46 AM
Hmmm...no.

My JSON is actually
{"success":true} , the rest - html wrapping - is added by extjs.
Maybe here is answer for my problem http://stackoverflow.com/questions/4859502/upload-files-by-extjs-ajax-request-and-asp-net-mvc-without-reload-page-ajax-sty, but when I change response contentType to "text/html"
struts action returns the whole page as response but extjs still expects to get JSON.

Regards,
Luke

stimpy
3 May 2012, 6:20 AM
I had the same probelm and here is what i found

The behaviour you experienced is actually documented (quite well buried, though) in ExtJS API docs - see description of http://docs.sencha.com/ext-js/4-1/#!/api/Ext.form.Basic-method-hasUpload

In short, file uploads are not performed by typical Ajax XMLHttpRequests. It uses a hidden iframe element instead. As a result, to quote ExtJS documentation:


If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html"...


So the problem is in Ext.

I got bit by this using Spring so I suggest Struts is responding similarly

stimpy
3 May 2012, 7:30 AM
For those that need it

Here is Spring handler to solve this problem for Spring pre 3.1. Post 3.1 you can set the content type in the @ResponseBody



@RequestMapping(value="/some/url/", method=RequestMethod.GET)
public ResponseEntity<String> handleAJAX() { HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "text/html; charset=utf-8");

//some stuff here to produce output
return new ResponseEntity<String>("{\"success\":true}", responseHeaders, HttpStatus.CREATED); }