I am using ajaxupload.js from here and I see the file doing the upload work alright. But I am getting <pre style="word-wrap: break-word; white-space: pre-wrap;">{"id":"006","path":"006.png"}</pre> in the response.

I think the response should be just {"id":"006","path":"006.png"} but for some reasons it got wrapped around <pre> and hence the Uncaught SyntaxError: Unexpected token <.

I am using spring mvc 3, tomcat. I am using java.io.Writer to write the response as writer.write(json.toString());

Could someone help me understand this error and how to resolve it?

Thanks.

UPDATE:

CODE:

<form id="app-form" class="cols" action="#" method="POST">
    <fieldset class="w50">                              
        <!--  set of form fields -->
    </fieldset>   
    <fieldset class="w50">                              
        <button id="uploadButton" class="csbutton-grey" >Upload</button>
        <ul id="locationImages"></ul>
    </fieldset>
<div style="float: left;">
    <button type="submit" class="cool-button">Submit</button>
</div>
</form>


$(document).ready(function(){
    var button = $('#uploadButton'), interval;

    new AjaxUpload(button, {
        action: 'uploadImage', 
        name: 'qqfile',
        responseType: "json",
        onSubmit : function(file, ext){
            this.disable();
            console.log("file - " + file);
            console.log("ext - " + ext);
            if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
                alert('Error: invalid file extension');
                return false; 
            }
            else {
                $.ajax({
                    type:"GET",
                    url:"file",
                    data:'file='+file,
                    success:function(data, textStatus, jqXHR){
                        console.log(jqXHR.status);
                        console.log(data);
                    },
                    error:function(jqXHR, textStatus, errorThrown) {
                        console.log(jqXHR.status);
                    },
                });                 
            }
        },
        onComplete: function(file, response){
            this.enable();
            console.log("file - " + file);
            console.log("response.id - " + response.id + ", response.path - " + response.path);
            $('<li></li>').appendTo('#locationImages').text(file);                      
        }
    }); 
});
有帮助吗?

解决方案

Have you set the responseType property as json in AjaxUpload?

其他提示

If you want to send JSON to the client, use Jackson. Spring MVC had native support for it. Create a bean class like this:

public class Result{
private String id;
private String path;
public Result(String id, String path){
this.id=id;this.path=path;}
public Result(){}
// add getters and setters
}

Now create your controller method like this

@ResponseBody // this is important
@RequestMapping("/path/to/mapping")
public Result someMethodName(SomeParameter param1, SomeParameter param2){
    // do something here
    return new Result(id, path);
}

As long as you have Jackson on your classpath and have configured your Spring app through <mvc:annotation-config />, this will automatically serialize your response Object to correct JSON (including correct mime type)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top