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

时间:2015-02-25 16:04:40

标签: javascript ruby-on-rails angularjs

当我从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]"}"

提前致谢。

3 个答案:

答案 0 :(得分:0)

这应该有所帮助:

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:-1)

是ruby中角度必需的实现模块吗?

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

我的IONIC是客户......

我的创建http.post方法是这样做的:

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

但应该是:

参数:{" utf8" =>"✓"," authenticity_token" =>" l916wPHn7wvf0LzEPclKBHALEE7j9KjyiUc5aMpatSFqyQGsmCLwN9ByT8gpwrcrEgC3MLRcafQWSc3V6bvAZA ==", "消失" => {"名称" =>" nome"},"提交" =>"保存&# 34;}

相关问题