angular.js$http.post将数据发送到Rails4

angular.js $http.post sends data to Rails 4

本文关键字:Rails4 数据 js http post angular      更新时间:2024-06-06

当我从angular发出创建会话的后期请求时

$http.post('http://localhost:3000/api/v1/sessions',                
            {
                email: $scope.email,
                password: $scope.password
            }).success(function(data, status, headers, config) {
                console.log(data);
                console.log(status);
            }).error(function(data, status, headers, config) {
                console.log(status);
            });
};

它以JSON形式接收,但params在rails中转换为哈希,那么如何获取电子邮件和密码的值呢?

Started POST "/api/v1/sessions" for 127.0.0.1 at 2015-02-25 21:21:56+0530
Processing by Api::V1::SessionsController#create as JSON
Parameters: {"{'"email'":'"hello@example.com'",'"password'":'"somepassword123'"}"=>"[FILTERED]"}

正如我现在看到的params内容-它是

"{'"email'":'"hello@example.com'",'"password'":'"somepassword123'"}"=>"[FILTERED]"}"

提前谢谢。

这应该会有所帮助:

$http({
    url: 'http://localhost:3000/api/v1/sessions',
    dataType: 'json',
    method: 'POST',
    data: {
        email: $scope.email,
        password: $scope.password
    },
    headers: {
        "Content-Type": "application/json"
    }
    }).success(function(response){
        // do something
    }).error(function(error){
        // do something
    });

我使用jQuery:解决了我的问题

  var myDataOfServer = $.param({'disappeared':{
               name:$scope.data.name ... }

$.param负责为服务端解析JSON的原因。

是否需要在angular中为ruby实现模块?

我的服务器Ruby是一个Web服务。

我的IONIC是客户。。。

我的createhttp.post方法是这样做的:

参数:{"{''"name ''":''"ola mundo ''"}"=>nil}

但应该是:

参数:{"utf8"=>"✓","authenticity_token"=>"l916wPHn7wvf0LzEPclKBHALEE7j9KjyiUc5aMpatSFqyQGsmCLwN9ByT8gpwrcrEgC3MLRcafQWSc3V6bvAZA==","消失"=>{"name"=>"nome"},"commit"=>"已保存"}