以下代码生成 406 Not Acceptable 错误。
我的代码有什么问题?
def remote_create
@photo = Photo.new(params[:photo])
respond_to do |format|
if @photo.save
# add @photo's thumbnail to last import tag
format.js {
render :update do |page|
page.insert_html :bottom, 'polaroids' , :partial => 'polaroid', :locals => {:photo => @photo}
end
}
else
format.html
end
end
end
Started POST "/photos/remote_create" for 127.0.0.1 at 2010-03-14 14:02:08
Processing by PhotosController#remote_create as HTML
Parameters: {"photo"=>{"photo"=>#<File:/var/folders/BT/BTpdsWBkF6myaI-sl3+1NU+++TI/-Tmp-/RackMultipart20100314-285-1y9eq1x-0>, "name"=>"4204497503_a0c43c561d.jpg"}}
SQL (0.6ms) INSERT INTO "photos" ("created_at", "filename", "height", "name", "photo_content_type", "photo_file_name", "photo_file_size", "photo_updated_at", "size", "updated_at", "width") VALUES ('2010-03-14 13:02:08.449499', NULL, NULL, '4204497503_a0c43c561d.jpg', 'application/octet-stream', '4204497503_a0c43c561d.jpg', 136710, '2010-03-14 13:02:08.446370', NULL, '2010-03-14 13:02:08.449499', NULL)
[paperclip] Saving attachments.
[paperclip] saving /Users/denisjacquemin/Documents/code/projects/photos/public/system/photos/224/original/4204497503_a0c43c561d.jpg
Completed in 101ms with 406
这是请求标题
Host localhost:3000
User-Agent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type multipart/form-data; boundary=AJAX-----------------------1268573300187
Referer http://localhost:3000/photos/
Content-Length 2058592
Cookie remember_token=ea5af5a7c9a38d72bfd07756754af682a5d16cac; _photos_session=BAh7ByIQX2NzcmZfdG9rZW4iMW1yTUo3RkhzRlhPUXhsa0ptdDAyaUpDcXlTdlU0OHk2WHJhUzBzMmVQV1k9Ig9zZXNzaW9uX2lkIiViNzc0MGI2ZGMyYWNlNjEzZWEwYjVhM2U1Njg1MWE3YQ%3D%3D--1af96dbdbef595c48121e4f5c16298cadeef5b2a
答案 0 :(得分:2)
我找到了解决方案,当我构建ajax请求时,我需要设置以下头参数
xhr.setRequestHeader("Accept", "text/javascript");
在设置此参数之前,请求标头为
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
现在请求标头看起来像这样
Accept text/javascript
现在正常工作
goo更新:
以下是完整代码,setRequestHeader
位于发送功能
希望有所帮助
在我看来
<%= link_to 'upload all', '/photos/remote_create', :remote => true, :method => 'POST', :multipart => 'droppedItems', :id => 'upload', :class => 'awesome small green' %>
在rails.js函数handleRemote(element)
中else if (element.readAttribute('multipart') != null) {
url = element.readAttribute('data-url') || element.readAttribute('href');
var uploader = new Uploader(null, $(element.readAttribute('multipart')));
uploader.sendFiles(url);
return;
}
在js实用程序文件中
sendFiles : function(url) {
try {
this.filesToUpload.forEach(function(file, index, all) {
send(file, url);
});
} catch(e) {
alert('send Error: ' + e);
}
}
function send(file, url) {
try {
var xhr = new XMLHttpRequest;
var boundary = generateBoundary();
var contentType = "multipart/form-data; boundary=" + boundary;
xhr.upload.addEventListener("loadstart", (function(e){
$('progress_'+file.name).update('0%');
}), false);
xhr.upload.addEventListener("progress", (function(e) {
if (e.lengthComputable) {
var percentage = Math.round((e.loaded * 100) / e.total);
$('progress_'+file.name).update(percentage + '%');
}
}), false);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
eval(xhr.responseText || '');
}
};
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", contentType);
xhr.setRequestHeader("Accept", "text/javascript");
var CRLF = "\r\n";
var request = "--" + boundary + CRLF;
request += 'Content-Disposition: form-data; ';
request += 'name="' + 'photo[name]' + '"' + CRLF + CRLF;
request += file.name + CRLF;
request += "--" + boundary + CRLF;
request += 'Content-Disposition: form-data; ';
request += 'name="' + 'photo[photo]' + '"; ';
request += 'filename="'+ file.fileName + '"' + CRLF;
request += "Content-Type: application/octet-stream" + CRLF + CRLF;
request += file.value + CRLF;
request+= "--" + boundary + "--" + CRLF;
xhr.sendAsBinary(request);
} catch(e) {
alert('send Error: ' + e);
}
}
答案 1 :(得分:1)
您是在设置特定格式(如使用jQuery,如{format: 'raw'}
),还是设置请求标头mime类型(搜索setRequestHeader
)?
最简单的方法是使用Firebug并查看您的请求是什么样的。
要尝试的其他事项,是否会在其他浏览器中出现同样的错误?