【发布时间】:2015-04-12 22:47:36
【问题描述】:
我有一个 Angular.js 应用程序,它向 Rails 服务器端点发送 POST 请求,但在传递和读取参数时遇到问题。
这就是我发送数据的方式
$scope.submitForm = function(info) {
$http({
method: 'POST',
url: app.apiServerUrl + '/complaints/create',
data: info,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
传递的info对象是一个形式为:{ title: "something", ocurrence_place: "something" }的对象。
在我的控制器create 操作中,我有这个:
def create
p params
p params[:title]
end
但这是打印在日志中的内容
{"{\"title\":\"foo\",\"ocurrence_place\":\"bar\"}"=>nil, "controller"=>"complaints", "action"=>"create"}
nil
所以,如您所见,我无法访问 params 哈希属性。我该如何解决这个问题,以便我的控制器中的控制器工作并打印预期的内容?
【问题讨论】:
标签: ruby-on-rails json angularjs angular-http