我使用的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