在本地主机和heroku上对xhr请求的不同响应

时间:2019-02-02 12:04:40

标签: ruby-on-rails heroku

这是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?,但这没什么不同。

0 个答案:

没有答案