2012-06-05 46 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

相關問題