为啥我要传递 'things[]' : ['apple', 'pillow'] 表单数据?

Posted

技术标签:

【中文标题】为啥我要传递 \'things[]\' : [\'apple\', \'pillow\'] 表单数据?【英文标题】:Why am I passing 'things[]' : [ 'apple', 'pillow'] form data?为什么我要传递 'things[]' : ['apple', 'pillow'] 表单数据? 【发布时间】:2016-02-15 17:56:30 【问题描述】:

我在 node.js / mongoose 中有一个非常简单的模式,如下所示:

var thingSchema = mongoose.Schema(
    thing_name  :  String,
    ting_logo  :  String,
    things : [String]
);

这是我在 angular.js 控制器中的 add 方法:

    $scope.submit_thing  = function() 
        var data = $.param( 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things );
        $http.post("http://10.0.0.2:8080/thing/add",
            data,
            headers: 'Content-Type': 'application/x-www-form-urlencoded').then(function(response) 
            console.log(response);
            console.log(response.data);
        );
        ...    

...但是当我调用$scope.submit_thing我的node.js 应用程序输出这个...

 thing_name: 'eeeee',
  thing_logo: 'eeeee',
  'things[]': [ 'Fishing', 'Mountain Biking' ] 

...然后我可以在我的数据库中找到这条记录,但是things 数组是空的。 [] 到底是从哪里来的?我认为它弄乱了我的表单数据。我的表单数据不应该是吗?

 thing_name: 'eeeee',
  thing_logo: 'eeeee',
  'things': [ 'Fishing', 'Mountain Biking' ] 

更新:我换行了...

        var data = $.param( 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things );

...到...

        var data =  'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things ;

...但这似乎让事情变得更糟了。现在我从我的 node.js 控制台得到这个输出......

 '"thing_name":"thing","thing_logo":"thing","things":["Running","Fishing"]': '' 

** 更新 2**:

好的,我现在在我的 thingCtrl.js 文件中执行此操作:

    $scope.submit_thing  = function() 
        var data = $.param( "thing_name": $scope.newthing, "thing_logo": $scope.newlogo, "things": JSON.stringify($scope.selected.things));

...所以知道一条完整的记录正在进入我的 mongodb,但 mongo 记录如下所示:

> db.things.find()
 "_id" : ObjectId("5646bb2e4235cd620080a969"), "thing_name" : "test001", "thing_logo" : "testlogo", "things" : [ "[\"Skiing\",\"Mountain Biking\"]" ], "__v" : 0 
> 

数组things 中的字符串是否有引号转义? 这是怎么回事?

【问题讨论】:

你试过 var data = thing_name: $scope.newthing, thing_logo: $scope.newlogo, things: $scope.selected.things 吗? 我认为问题在于 uu 传递给事情为真的服务器对象。你应该像这样传递 smth:name: 'Name', logo: 'Logo', things: ['Fishing']。你可以试试这个吗? 【参考方案1】:

好的,这就是我必须做的......

  $scope.submit_thing  = function() 
        var data = Object( 'thing_name': $scope.newthing, 'thing_logo': $scope.newlogo, 'things': $scope.selected.things);
        $http.post("http://10.0.0.2:8080/vendor/add",
            JSON.stringify(data)
            //headers: 'Content-Type': 'application/x-www-form-urlencoded'
         ).then(function(response) 
            console.log(response);
            console.log(response.data);
        );

我认为所做的事情是注释掉标题行。

【讨论】:

以上是关于为啥我要传递 'things[]' : ['apple', 'pillow'] 表单数据?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我要添加 rax 和 rdx?

为啥我要截屏它说是因为安全政策限制不能截屏

为啥我要传输 `client_secret` 来获取 `access_token`?

为啥通过 Vue-router 传递数据返回“[object Object]”?

apache_conf 小的ruby脚本在jira中获取jql查询(高级搜索)的结果,并为Cultured Code的Things App创建todo项目。我用

为啥在首先合并实体时将已删除的实例传递给合并