我使用的ajaxupload.js
從here,我看到上傳文件正常工作。但我得到<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);
}
});
});
我完全錯過了設置。現在已經做了錯誤消失了,但我得到'[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
但是使用Safari 5.1.4和Chrome 19.0.1084.52 m,我得到'[object Object ]'的響應和它不工作與IE 8或Mozilla 9.我該怎麼辦?我使用https://github.com/valums/ajax-upload。 – skip
如果你提醒一個JSON對象,你會得到[object對象],這意味着你已經得到適當的對象了,你可以嘗試通過obj.id,obj.path來獲取你正在獲取的合適的值 – Dandy