【问题标题】:Why is Angular http.post converting string value to an array?为什么 Angular http.post 将字符串值转换为数组?
【发布时间】:2017-03-28 14:45:11
【问题描述】:

我有一个 Angular JS 和 Laravel 应用程序。我的表单字段之一是ng-options 驱动选择。这些值是字符串,但是当我使用 http.post 发布时,其中一个值似乎正在转换为数组。

我的 JS 控制器中有这个选项;

    $scope.periods = [
    {label: '1'},
    {label: '2'},
    {label: '3'},
    {label: 'OT'}
];

这是我对选择的看法;

<select ng-model="egoal.period"
        ng-options="period.label as period.label for period in periods"
        class="form-control game-control"
        required>
</select>

然后这个在控制器中发布;

  $scope.changeGoal = function() {
        var egoal = this.egoal;
        $http.post('api/editGoal/' + this.goal.id, egoal)
            .success(function () {
                $scope.egoal = {};
            });
};

奇怪的是,当我转储$request 时,我得到period =&gt; [] 然后这个错误;

helpers.php 第 685 行中的错误异常: preg_replace():参数不匹配,pattern是字符串而replacement是数组

关注这个很久了。这可能是什么原因造成的?
谢谢!

更新: 选择后egoal对象是这样的;

EGoal: {
  "id": 268,
  "game": 147,
  "season": 4,
  "team_id": 2,
  "period": "2",
  "min": 7,
  "sec": 54,
  "scorer": 11,
}

但周期值在发布时转换为空数组...

【问题讨论】:

    标签: angularjs laravel-5


    【解决方案1】:
      $scope.changeGoal = function() {
            var egoal = this.egoal;
            $http.post('api/editGoal/' + this.goal.id, egoal)
    

    javascript 中的函数是具有自己作用域的块。您需要使用vm 概念。 https://github.com/johnpapa/angular-styleguide/tree/master/a1#controlleras-with-vm

    在 changeGoal 函数的上下文中,this.egoal 是未定义的。

    将 undefined as data 发送到 $http.post 以某种方式将其序列化为一个空数组。

    【讨论】:

    • 可能是因为php“对象”被称为数组?也许不在 Laravel 中。
    • 好的,我在关注。就在加载 changeGoal 表单之前,我声明了 egoal。所以我实际上可以将 egoal 打印到视图中,并看到表单和模型绑定按预期工作 - 更改 egoal.period 会在模型中更改它。在我发帖之前一切都很好。
    • 在 changeGoal 函数中,试试 console.log(this.egoal)。它将是未定义的。 javascript 中的函数是具有自己作用域的块。您需要使用vm 概念github.com/johnpapa/angular-styleguide/tree/master/…
    • 啊!到达某个地方。不是未定义的,但时期是作为一个对象而不是一个值来的。怎么样?
    • 我不知道你在看什么或在哪里。可以确定的是,如果该函数与您的问题中的完全相同,那么 this.egoal 在传递给 $http.post 时是未定义的。使用 $scope.egoal 而不是 this.egoal 如果它仍然不起作用,请使用小提琴重现您的问题。
    【解决方案2】:

    来自 ngOptions 文档:

    当菜单中的一个项目被选中时,被选中的选项所代表的数组元素或对象属性将被绑定到ngModel指令所标识的模型上。

    因此,当您从下拉列表中选择其中一项时,您的 ngModel egoal.period 不是 1,或 2,或 3,或 OT,而是 {label: '1'}, {label: '2'},等等。实际上是指向 $scope.periods 数组中的一项的指针。

    查看这个 plunker 以更好地理解我的意思: http://embed.plnkr.co/9cJ2Hy/

    您的 PHP 脚本可能需要这样的 json 对象:

    {
      ...
      "period": '1',
      ...
    }
    

    但它实际上变得更像这样:

    {
      ...
      "period": "{\"label\": '1'}",
      ...
    }
    

    【讨论】:

    • 嘿乔尔CDoyle。我正在关注,但我使用 ng-model='egoal.period' 进行选择。因此,当我将 egoal 对象打印到我的视图时,我确实将句点视为属性,将标签视为值。你是说这实际上不是正在发生的事情吗?
    • 你会发布打印出来的自我对象的样子吗?您可以将其限制在相关部分。
    • 查看有问题的更新。
    • 好吧,这个答案是不对的。既然您使用的是period.label as period.label,那么您已经做对了。
    • 谢谢 - 反正我以为我是 - 仍然难倒。顺便说一句 - 你是上校 Les Claypool 的死侍!
    【解决方案3】:

    AngularJS 在 $http.post 中默认使用 JSON 对象。

    您可以通过向配置对象添加正确的 Content-Type 标头来更改默认设置:

    headers : { 'Content-Type': 'application/x-www-form-urlencoded' } 
    

    更新:

    ...只需添加:

    angular.module('myApp', [])
            .config(function ($httpProvider) {
                $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
                $httpProvider.defaults.headers.post['Content-Type'] =  'application/x-www-form-urlencoded';
            })
    

    或者另一种解决方案是:

    return $http({
             method: 'POST',
             url: "/yoururl/",
             data:{
                'param1': "Test",
             },
             headers: {
                 'Content-Type': 'application/x-www-form-urlencoded'
             }}).then(function(result) {
                 console.log(result);
             }, function(error) {
                 console.log(error);
             });
    

    【讨论】:

    • 听起来不错。通过向配置对象添加正确的 Content-Type 标头,您的意思有点困惑:
    • 好的,补充一下。现在当我转储我的请求时,我得到一个数组,句号现在是一个空对象。
    • array:1 [ "{"id":268,"game":147,"season":4,"team":"Olden_Hawks","team_id":2,"period": {},"min":7,"sec":54,"scorer":11,"scorerId":91,"assist1":13,"assist1Id":null,"assist2":null,"assist2Id":null ,"空":0,"short":0,"created_at":"2016-09-12_20:35:04","updated_at":"2016-11-14_15:59:08"}" => "" ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2019-04-14
    相关资源
    最近更新 更多