【问题标题】:http put request causes "net::ERR_TOO_MANY_REDIRECTS" errorhttp put 请求导致“net::ERR_TOO_MANY_REDIRECTS”错误
【发布时间】:2016-08-12 16:49:28
【问题描述】:

我有一个 Angular 客户端向 Rails 后端发出 http put 请求。

http请求代码如下:

    $http({
        method: 'PUT',
        url: 'http://localhost:3000/documents/' + $scope.document_id,
        data: parameter
    }).then(function successCallback(response) {
            console.log(response);
        }, function errorCallback(response) {
            console.log("Error has occurred while getting user document data from the server");
        });

我传递的数据如下,它是一个有效的 JSON:

        var parameter = {
  "document": {
    "title": "I-129",
    "data": JSON.stringify($scope.formData),
    "user_id": 1
  }
}

后端代码如下:

  def update
    respond_to do |format|
      if @document.update(document_params)
        format.html { redirect_to @document, notice: 'Document was successfully updated.' }
        format.json { render :show, status: :ok, location: @document }
      else
        format.html { render :edit }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

我正在获取的服务器日志:

Started PUT "/documents/3" for ::1 at 2016-04-20 00:24:12 -0700
Processing by DocumentsController#update as HTML
  Parameters: {"document"=>{"title"=>"I-129", "data"=>"{\"text_id\":\"1111\",\"Date_id\":\"1111-11-11T08:00:00.000Z\",\"number_id\":11111222}", "user_id"=>0}, "id"=>"3"}
Can't verify CSRF token authenticity
  Document Load (0.1ms)  SELECT  "documents".* FROM "documents" WHERE "documents"."id" = ? LIMIT 1  [["id", 3]]
   (0.0ms)  begin transaction
  Document Exists (0.1ms)  SELECT  1 AS one FROM "documents" WHERE ("documents"."title" = 'I-129' AND "documents"."id" != 3) LIMIT 1
   (0.0ms)  commit transaction
Redirected to http://localhost:3000/documents/3
Completed 302 Found in 3ms (ActiveRecord: 0.3ms)

每当我发出 put 请求时,我都会收到一条错误消息

http://localhost:3000/documents/3net::ERR_TOO_MANY_REDIRECTS

我在这里做错了什么?我确认我可以通过 Paw(类似于 Postman 的程序)在 URL('http://localhost:3000/documents/' + $scope.document_id) 上执行 http put 请求。

【问题讨论】:

  • 请同时显示后端代码。
  • 在正文中添加。
  • 在后端日志中,您可以查看 rails 应用程序正在呈现 html 还是 json。您可以验证或发布日志吗?
  • 它渲染了两种类型的文件。最后,它呈现 index.html。

标签: ruby-on-rails angularjs json http request


【解决方案1】:

从日志中可以看到 rails 将调用视为 HTML

Processing by DocumentsController#update as HTML

尝试将其添加到 http 调用中

 dataType: "json",
 headers: {
     "Content-Type": 'application/json; charset=UTF-8',
     "Accept" : "application/json"
 }

【讨论】:

  • 还是没有运气。给我同样的错误。我注意到我在文档中的 user_id 在错误发生后设置为 0。并且日志保持不变。
  • 后端在rails中,所以我们可以尝试一些东西...尝试将.json添加到您的url
  • 在标题中添加 Accept(见修改后的答案)
  • 如果我在最后添加“.json”,它确实会更改数据而不会出现错误。但它一直在删除文档中的user_id。有什么想法吗?
【解决方案2】:

试试这个:

url: 'http://localhost:3000/documents/' + $scope.document_id + '.json'

def update
respond_to do |format|
  if @document.update(document_params)
    format.html { redirect_to @document, notice: 'Document was successfully updated.' }
    format.json { render json: @document, status: :ok}
  else
    format.html { render :edit }
    format.json { render json: @document.errors, status: :unprocessable_entity }
  end
end

结束

【讨论】:

    【解决方案3】:

    试试下面的:

    dataType: "json",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2012-02-28
      相关资源
      最近更新 更多