【发布时间】:2012-07-12 03:55:29
【问题描述】:
我正在上传一些 HTML5,但遇到了一个我似乎无法弄清楚的问题。拿这个功能代码:
if @image.save
render :json => {:status => "Successfully uploaded image!"}
else
render :json => {:status => "Something went wrong with your upload!"}
end
这行得通,而且它完全按照它应该做的那样做。但是,当我添加 respond_to 来捕获 HTML 请求时,它完全失败了(尽管 HTML 部分有效。)
respond_to do |format|
if @image.save
format.html {redirect_to(edit_product_path(@image.product), :notice => "Your image was added successfully.")}
format.json {render :status => "Successfully uploaded image!"}
else
format.html {redirect_to(edit_product_path(@image.product), :alert => "An error occurred while attempting to upload your image. Please try again.")}
format.json {render :status => "Something went wrong with your upload!"}
end
end
有什么想法可能是错的吗?我觉得我忽略了一些简单的事情。谢谢。
编辑 - 问题已解决
我认为这是我一直忽略的一件愚蠢的事情。原来请求是 HTML,而不是 JSON,这是因为上传需要内容类型为 multipart/form-data,respond_to 触发了 HTML。
【问题讨论】:
-
Firebug 简单显示:JSON.parse: unexpected character return window.JSON.parse(data);
标签: ruby-on-rails ruby-on-rails-3 json