简体   繁体   中英

http put request causes “net::ERR_TOO_MANY_REDIRECTS” error

I have an Angular client issuing http put request to rails backend.

http request code is like following:

    $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");
        });

Data I'm passing is like following and it is a valid JSON:

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

Backend code is like folloiwng:

  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

Server log I'm getting:

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)

Whenever I issue put request, I get an error saying

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

What am I doing wrong here? And I confirm that I can do the http put request through Paw (similar program to Postman) on URL( 'http://localhost:3000/documents/' + $scope.document_id ).

From the log you can see that rails is treating the call as HTML

Processing by DocumentsController#update as HTML

Try adding this to the http call

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

try this:

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

and

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

end

Try the below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM