2012-06-05 91 views
0

我使用的ajaxupload.jshere,我看到上传文件正常工作。但我得到<pre style="word-wrap: break-word; white-space: pre-wrap;">{"id":"006","path":"006.png"}</pre>在回应。未捕获的SyntaxError:意外的令牌<

我认为回应应该只是{"id":"006","path":"006.png"},但由于某些原因,它包裹了<pre>,因此Uncaught SyntaxError: Unexpected token <

我使用的是spring mvc 3,tomcat。我正在使用java.io.Writer来写回复为writer.write(json.toString());

有人能帮我理解这个错误以及如何解决它吗?

谢谢。

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);      
     } 
    }); 
}); 

回答

1

您是否已将responseType属性设置为json in AjaxUpload?

+0

我完全错过了设置。现在已经做了错误消失了,但我得到'[object Object]'的响应和一个新的错误来我必须将'<!DOCTYPE html>'替换为'<!DOCTYPE html PUBLIC“ - // W3C // DTD HTML 4.01 Transitional // EN”“http://www.w3.org/TR/html4/loose .dtd“>”解决它,oth erwise IE 8并不高兴,页面的样式也随之出错。并且'https:// github.com/valums/ajax-upload'不适用于IE 8或Mozilla 9 :(。 – skip

+0

但是使用Safari 5.1.4和Chrome 19.0.1084.52 m,我得到'[object Object ]'的响应和它不工作与IE 8或Mozilla 9.我该怎么办?我使用https://github.com/valums/ajax-upload。 – skip

+1

如果你提醒一个JSON对象,你会得到[object对象],这意味着你已经得到适当的对象了,你可以尝试通过obj.id,obj.path来获取你正在获取的合适的值 – Dandy

1

如果你想JSON发送到客户端,使用Jackson。 Spring MVC对它有本地支持。创建一个bean类是这样的:

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 
} 

现在只要你有杰克逊在你的类路径,并通过<mvc:annotation-config />配置你的Spring应用程序创建一个这样

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

你的控制器的方法,这将自动序列化您的回应对象纠正JSON(包括正确的MIME类型)

+0

是的。这是我总是用来返回json数据,但与https://github.com/valums/ajax-upload我得到了'java.lang.IllegalStateException:getWriter()已被呼吁此响应 \t在org .apache.catalina.connector.Response.getOutputStream(Response.java:580) \t在org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183) \t在org.springframework.http.server.ServletServerHttpResponse .getBody(ServletServerHttpResponse.java:71)...':( – skip

相关问题