AngularJS $资源发出了一个额外的"注册"哈希? [英] AngularJS $resource sending out an extra "registration" hash?

查看:99
本文介绍了AngularJS $资源发出了一个额外的"注册"哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新来这里AngularJS并有一定的麻烦$资源工作。我创建了一个工厂,硬codeD什么我的后端,以创建一个新的水暖工需要$资源对象。

然而,当我打电话的功能,而不是只传递,我已经进入了PARAMS,它创建了一个在注册哈希的形式重复内容排序,如图我的应用程序的后台兴田(加粗下面)。这基本上是我的PARAMS完全相同的副本。它来自哪里?

的Create()函数是由管道工/ new.html模板

按钮叫

<按键数据-NG-点击=创建()>创建< /按钮>

如果这样做,哈希来自???

new.js

  angular.module('ngappApp')
.controller('PlumbersNewCtrl',函数($范围,$窗口,$资源,水管工){$ scope.create =功能(){
  VAR测试1 =新的管道工({
    企业[邮件]:superhero@super.com
    企业[密码]:123123,
    企业[名]:爱丽丝·卡伦
    企业(公司):爱丽丝PTY LTD
    企业[荷兰]:12312312,
    企业[contact_number]:0421772800,
    企业[地址]:118玻璃一条街,
    企业[郊区]:埃森
    企业[交code]:3040
    企业[employees_number]:8
  });  。TEST1 $保存();
};});angular.module('ngappApp')
.factory('管道工',函数($资源){
返回$资源('/企业');
});

和从后端的响应:


  

入门POST/企业为127.0.0.1在2013年8月26日20时40分27秒+1000
  通过企业加工:: RegistrationsController#创建为JSON
    参数:{的经营[邮件]=>superhero@super.com,企业[密码]=>[FILTERED],企业[名]=>爱丽丝·卡伦,企业[公司]=>爱丽丝PTY LTD,企业[荷兰]=>12312312,企业[contact_number]=>0421772800,企业[地址]=>118玻璃街道,企业[郊区]=>埃森,企业[交code]=>3040,企业[employees_number]=>8,登记=> {经营[邮件]=>superhero@super.com,企业[密码]=>[FILTERED],企业[名]=>爱丽丝·卡伦,企业(公司)=>爱丽丝PTY LTD,企业[荷兰]=>12312312,企业[contact_number]=>0421772800,企业[地址]=>118玻璃街道,企业[郊区]=> 埃森,企业[交code]=>3040,企业[employees_number]=>8}}
  警告:无法验证令牌CSRF真伪
     (0.1毫秒)开始交易
     (0.0ms)ROLLBACK TRANSACTION
  在6ms的完成了400错误的请求(浏览次数:0.1ms的| ActiveRecord的:为0.2ms)


编辑:

登录测试1显示了正确的PARAMS没有额外

 资源{业务[邮件]:superhero@super.com,企业[密码]:123123,业务【名称】:爱丽丝·卡伦,企业(公司) :爱丽丝PTY LTD
商业[荷兰]:12312312
商业[地址]:118玻璃一条街
企业(公司):爱丽丝PTY LTD
商业[contact_number]:0421772800
商业[邮件]:superhero@super.com
商业[employees_number]:8
商务【名称】:爱丽丝·卡伦
商业[密码]:123123
商业[交code]:3040
商业[郊区]:埃森


解决方案

这不是一倍。 Rails是你的包裹的PARAMS注册参数里面,但你同时看到,因为它仍然允许通过。我也on Rails的写作angularjs应用程序。

顺便说一句,你为什么你的包裹code这样呢?如果你想将其包装散列里面,那么你需要做的:

业务= {名:姓名,胡说:嗒嗒}

New to AngularJS here and have some trouble getting $resource to work. I've created a factory and hardcoded the $resource object with what my backend needs in order to create a new plumber.

However, when I call the function, instead of passing only the params that I've entered, it creates a 'sort of' duplicate content in the form of a registration hash, as shown in hte backend of my app (bolded below). It's basically an exact duplicate of my params. Where did it come from???

The create() function is called by a button in the plumbers/new.html template

<button data-ng-click="create()">create</button>

Where did that hash come from???

new.js

angular.module('ngappApp')
.controller('PlumbersNewCtrl', function ($scope, $window, $resource, Plumbers) {

$scope.create = function(){
  var test1 = new Plumbers({
    "business[email]": "superhero@super.com",
    "business[password]": "123123",
    "business[name]": "alice cullen",
    "business[company]": "alice pty ltd",
    "business[abn]": "12312312",
    "business[contact_number]": "0421772800",
    "business[address]": "118 glass street",
    "business[suburb]": "essendon",
    "business[postcode]": "3040",
    "business[employees_number]": "8"
  });

  test1.$save();
};

});

angular.module('ngappApp')
.factory('Plumbers', function($resource){
return $resource('/businesses');
});

And the response from the backend:

Started POST "/businesses" for 127.0.0.1 at 2013-08-26 20:40:27 +1000 Processing by Businesses::RegistrationsController#create as JSON Parameters: {"business[email]"=>"superhero@super.com", "business[password]"=>"[FILTERED]", "business[name]"=>"alice cullen", "business[company]"=>"alice pty ltd", "business[abn]"=>"12312312", "business[contact_number]"=>"0421772800", "business[address]"=>"118 glass street", "business[suburb]"=>"essendon", "business[postcode]"=>"3040", "business[employees_number]"=>"8", "registration"=>{"business[email]"=>"superhero@super.com", "business[password]"=>"[FILTERED]", "business[name]"=>"alice cullen", "business[company]"=>"alice pty ltd", "business[abn]"=>"12312312", "business[contact_number]"=>"0421772800", "business[address]"=>"118 glass street", "business[suburb]"=>"essendon", "business[postcode]"=>"3040", "business[employees_number]"=>"8"}} WARNING: Can't verify CSRF token authenticity (0.1ms) begin transaction (0.0ms) rollback transaction Completed 400 Bad Request in 6ms (Views: 0.1ms | ActiveRecord: 0.2ms)

EDIT:

Logging test1 shows the correct params without the 'extra'

Resource {business[email]: "superhero@super.com", business[password]: "123123",                                           business[name]: "alice cullen", business[company]: "alice pty ltd",
business[abn]: "12312312"
business[address]: "118 glass street"
business[company]: "alice pty ltd"
business[contact_number]: "0421772800"
business[email]: "superhero@super.com"
business[employees_number]: "8"
business[name]: "alice cullen"
business[password]: "123123"
business[postcode]: "3040"
business[suburb]: "essendon"

解决方案

It's not doubled. Rails is wrapping your params inside of the registrations parameter, but you see both, cause it still lets that through. I'm also writing an angularjs app on Rails.

BTW, why are you wrapping your code like that? If you want to wrap it inside of a hash then you need to do:

business = {name: "Name", blah:"Blah"}

这篇关于AngularJS $资源发出了一个额外的&QUOT;注册&QUOT;哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆