yuwei5380 / jsonplugin

Automatically exported from code.google.com/p/jsonplugin
0 stars 0 forks source link

Upload file: Cannot receive json response when using jquery.form, struts-2.1.6, jsonplugin 0.3.3 #80

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. I upload file by using jquery.form (http://malsup.com/jquery/form/)
2. I use struts2.1.6 + jsonplugin-0.3.3 for upload functionality.
3. I upload file successfully but jquery.form can not receive json response
content.

What is the expected output? What do you see instead?
json's response needs wrapping by <textarea> tag. 

Struts configuration will be
<action name="UploadResource"
class="springResourcesUploadResourceAJAXAction" method="executeUploadResource">
    <result name="input" type="json">
        <param name="wrapWithAreaComments">true</param>
        <param name="excludeProperties">
            model.*
        </param>
    </result>
    <result type="json">
        <param name="wrapWithAreaComments">true</param>
        <param name="excludeProperties">
            model.cacheData,
            model.resourceFile
        </param>
    </result>
</action>

What version of the product are you using? On what operating system?

Please provide any additional information below.

Fixing code is in jsonplugin-jquery.form-areawrapper.zip attachment
This code also fix for issue
http://code.google.com/p/jsonplugin/issues/detail?id=79 (NoSuchMethodError:
com.opensymphony.xwork2.ActionContext.get (struts-2.1.6, jsonplugin 0.3.3))

Here is description from jquery.form (http://malsup.com/jquery/form/ -->
select "Sample Code" tab --> select "File Uploads" tab.

 This page demonstrates the Form Plugin's file upload capabilities. There
is no special coding required to handle file uploads. File input elements
are automatically detected and processed for you.

Since it is not possible to upload files using the browser's XMLHttpRequest
object, the Form Plugin uses a hidden iframe element to help with the task.
This is a common technique, but it has inherent limitations. The iframe
element is used as the target of the form's submit operation which means
that the server response is written to the iframe. This is fine if the
response type is HTML or XML[1], but doesn't work as well if the response
type is script or JSON, both of which often contain characters that need to
be repesented using entity references when found in HTML markup.

To account for the challenges of script and JSON responses, the Form Plugin
allows these responses to be embedded in a textarea element and it is
recommended that you do so for these response types when used in conjuction
with file uploads. Please note, however, that if a file has not been
selected by the user for the file input then the request uses normal XHR to
submit the form (not an iframe). This puts the burden on your server code
to know when to use a textarea and when not to. If you like, you can use
the iframe option of the plugin to force it to always use an iframe mode
and then your server can always embed the response in a textarea. The
following response shows how a script should be returned from the server:

<textarea> 
    for (var i=0; i < 10; i++) { 
        // do some processing 
    } 
</textarea>

Original issue reported on code.google.com by tungnq.n...@gmail.com on 17 Jan 2009 at 4:59

Attachments:

GoogleCodeExporter commented 8 years ago
I committed some changes to trunk which add a "wrapPrefix" and "wrapSuffix"
atrtibutes to the JSON result, and can be used to generate the desired wrappers

Original comment by musa...@gmail.com on 28 Jul 2009 at 6:11