Ruby on rails http put请求原因“;net::ERR“u太多”重定向;错误
我有一个Angular客户端向rails后端发出http put请求 http请求代码如下所示:Ruby on rails http put请求原因“;net::ERR“u太多”重定向;错误,ruby-on-rails,angularjs,json,http,request,Ruby On Rails,Angularjs,Json,Http,Request,我有一个Angular客户端向rails后端发出http put请求 http请求代码如下所示: $http({ method: 'PUT', url: 'http://localhost:3000/documents/' + $scope.document_id, data: parameter }).then(function successCallback(response) { console.log
$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请求时,我都会收到一个错误消息
net::ERR\u太多\u重定向
我做错了什么?我确认我可以通过URL(
)上的Paw(类似于邮递员的程序)执行http put请求http://localhost:3000/documents/“+$scope.document_id
)。从日志中可以看到rails将调用视为HTML
Processing by DocumentsController#update as HTML
尝试将其添加到http调用
dataType: "json",
headers: {
"Content-Type": 'application/json; charset=UTF-8',
"Accept" : "application/json"
}
试试这个:
url: 'http://localhost:3000/documents/' + $scope.document_id + '.json'
及
结束尝试以下操作:
dataType: "json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
请向我们展示后端代码。将其添加到正文中。在后端日志中,您可以看到rails应用程序是否呈现html或json。您可以验证或发布日志吗?它会呈现这两种类型的文件。最后,它呈现index.html。仍然不走运。给我同样的错误。有一点我注意到文档中的用户id在错误发生后被设置为0。日志保持不变。后端在rails中,所以我们可以尝试一些东西。。。尝试将.json添加到您的urlAdd中,并在标题中添加Accept(请参阅修改后的answere)。如果我在末尾添加“.json”,它确实会更改数据,而不会出现错误。但它会不断删除文档中的
user\u id
。有什么想法吗?让我们谈谈。
dataType: "json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}