这是rails控制器代码:
respond_to do |format|
if @article.save
if request.xhr?
format.js { render json: @article.id }
else
format.html { redirect_to @article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: @article }
end
[...]
end
这是XMLHttpRequest:
var xhr = new XMLHttpRequest();
xhr.open('POST', "/articles/")
xhr.withCredentials = true;
var formData = new FormData();
formData.append('utf8', true);
formData.append('article[link]', "foo");
formData.append('article[title]', "bar");
formData.append('commit', "Create+Article");
xhr.setRequestHeader("X-requested-with", "XMLHttpRequest")
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log(xhr.response);
}
}
xhr.send(formData);
当我在localhost上运行请求并保存请求时,响应是应该的文章ID,但在heroku上的响应是“模板丢失”错误。
我尝试在head :ok
下添加format.html { redirect_to @article, notice: 'Article was successfully created.' }
和request.xhr?
,但这没什么不同。