【发布时间】: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