繁体   English   中英

将对象转换为URL编码的字符串时无法将对象转换为原始值

[英]Cannot convert object to primitive value in converting object into URL encoded string

我正在尝试使用以下功能将对象转换为URL编码的字符串:

console.log(jQuery.param($scope.employee));

console.log($.param($scope.employee));

但是他们给我一个错误:

TypeError:无法在Function.r.param(jquery)的Ab(jquery-3.2.1.min.js:4)的e(jquery-3.2.1.min.js:4)的encodeURIComponent()处将对象转换为原始值-3.2.1.min.js:4)在m。$ scope.save(employees.js:49)在fn(编译时的评估(angular.min.js:241),:4:388)在e(角度.min.js:286)位于m。$ eval(angular.min.js:149)位于HTMLButtonElement的m。$ apply(angular.min.js:150)。 (angular.min.js:286)

$scope.employee的值

Data: Object
contact_number:"112"
created_at:"2017-08-08 05:27:03"
email:"asd@111.com"
id:9
name:"re"
position:"a"
updated_at:"2017-08-08 05:27:03"

这是使用HomeStead上托管的Laravel和AngularJS开发的。 jQuery已安装。

更新仅用于验证目的:

模态HTML代码:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">{{form_title}}</h4>
</div>
<div class="modal-body">
<form name="frmEmployees" class="form-horizontal" novalidate="">
<div class="form-group error">
<label for="inputEmail3" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
    <input type="text" class="form-control has-error" id="name" name="name" placeholder="Fullname" value="{{employee.data.name}}" 
    ng-model="employee.data.name" ng-required="true">
    <span class="help-inline" 
    ng-show="frmEmployees.name.$invalid && frmEmployees.name.$touched">Name field is required</span>
</div>
</div>

<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Email</label>
<div class="col-sm-9">
    <input type="email" class="form-control" id="email" name="email" placeholder="Email Address" value="{{employee.data.email}}" 
    ng-model="employee.data.email" ng-required="true">
    <span class="help-inline" 
    ng-show="frmEmployees.email.$invalid && frmEmployees.email.$touched">Valid Email field is required</span>
</div>
</div>

<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Contact Number</label>
<div class="col-sm-9">
    <input type="text" class="form-control" id="contact_number" name="contact_number" placeholder="Contact Number" value="{{employee.data.contact_number}}" 
    ng-model="employee.data.contact_number" ng-required="true">
<span class="help-inline" 
    ng-show="frmEmployees.contact_number.$invalid && frmEmployees.contact_number.$touched">Contact number field is required</span>
</div>
</div>

<div class="form-group">
<label for="inputEmail3" class="col-sm-3 control-label">Position</label>
<div class="col-sm-9">
    <input type="text" class="form-control" id="position" name="position" placeholder="Position" value="{{employee.data.position}}" 
    ng-model="employee.data.position" ng-required="true">
<span class="help-inline" 
    ng-show="frmEmployees.position.$invalid && frmEmployees.position.$touched">Position field is required</span>
</div>
</div>

</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="btn-save" ng-click="save(modalstate, employee.data.id)" ng-disabled="frmEmployees.$invalid">Save changes</button>
</div>
</div>
</div>
</div>

注意:在本教程中,我关注的是ng-model的值是: employee.position但是将其更改为employee.data.position ,原因是当我更新记录时,值不会填充在字段中,而是添加data后,现在将填充值。

我才刚刚开始学习Laravel和AngularJS。

如果使用的是Angular,则可以使用$ httpParamSerializerJQLike ,示例代码:

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

Angular文档在这里

这可以不用jQuery而完成

var serialize = function(obj, prefix) {
  var str = [], p;
  for(p in obj) {
    if (obj.hasOwnProperty(p)) {
      var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
      str.push((v !== null && typeof v === "object") ?
        serialize(v, k) :
        encodeURIComponent(k) + "=" + encodeURIComponent(v));
    }
  }
  return str.join("&");
}

用法console.log(serialize($scope.employee))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM