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

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

问题描述

这里是 AngularJS 的新手,并且在让 $resource 工作时遇到了一些麻烦.我创建了一个工厂,并用我的后端需要的东西对 $resource 对象进行了硬编码,以便创建一个新的管道工.

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???

create() 函数由管道工/new.html 模板中的按钮调用

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

那个哈希是从哪里来的???

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

以及来自后端的响应:

在 2013-08-26 20:40:27 +1000 开始为 127.0.0.1 发布/businesses"由企业处理::RegistrationsController#create as JSON参数:{"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[郊区]"=>"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"}}警告:无法验证 CSRF 令牌的真实性(0.1ms) 开始事务(0.0ms) 回滚事务在 6 毫秒内完成 400 个错误请求(查看次数:0.1 毫秒 | ActiveRecord:0.2 毫秒)

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)

记录 test1 显示正确的参数,没有额外"

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"

推荐答案

它不是翻倍的.Rails 将您的 params 包装在 registrations 参数中,但您可以看到两者,因为它仍然允许它通过.我还在 Rails 上编写 angularjs 应用程序.

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"}

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

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

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