【问题标题】:ERROR TypeError: Converting circular structure to JSON错误类型错误:将循环结构转换为 JSON
【发布时间】:2018-09-17 03:56:23
【问题描述】:

我正在使用 Angular5。 我已经实现了以下代码以将数据作为 json 发送到 api,请参见下文

在 xxxx.component.ts 中

submitProfileData() {
           let registrationsData = [];
           for (let i = 0; i < this.profileModel.registrationData.length; i++ ) {
            registrationsData.push(
                {
                    id: this.profileModel.registrationData[i].id,
                    registration_number: this.profileModel.registrationData[i].regNo
                });
           }
           const primary_details = {
            street_line1: this.profileEditForm.controls.streetLine1.get('line1').value,
            street_line2: this.profileEditForm.controls.streetLine2.get('line2').value,
            country: this.profileEditForm.controls.countryName.get('country').value,
            state: this.profileEditForm.controls.stateName.get('state').value,
            };
           const profile_details = {
            name: this.profileEditForm.controls.branchName.get('name').value,
            tradingname: this.profileEditForm.controls.tradingName.get('trname').value,
            acronym: this.profileModel.acronym,
            timezone: this.profileEditForm.controls.timeZone,
            registrations: registrationsData
            };
            this.profileEditService
            .editProfileData({primary_details: primary_details, profile_details: profile_details}, this.branchId)
            .subscribe(suc => console.log('response', suc));

    }

在 xxxxx.service.ts 中

editProfileData(data, branchId) {
        console.log('request data', data);
        return this.http.post(this.config.STORE_KEY + '/public/api/business/changerequest/' + 
                 this.userDetails.roleid + '/' + branchId, data)
                 .map((response: Response) => response.json())
                 .catch(this.handleError);
    }

41 行控制台正在 chrome 控制台上打印。但我收到以下错误

ERROR TypeError: Converting circular structure to JSON
    at Object.stringify (<anonymous>)
    at Request.webpackJsonp.../../../http/@angular/http.es5.js.Body.text (http.es5.js:839)
    at Request.webpackJsonp.../../../http/@angular/http.es5.js.Request.getBody (http.es5.js:1748)
    at Observable._subscribe (http.es5.js:1275)
    at Observable.webpackJsonp.../../../../rxjs/Observable.js.Observable._trySubscribe (Observable.js:172)
    at Observable.webpackJsonp.../../../../rxjs/Observable.js.Observable.subscribe (Observable.js:160)
    at CatchOperator.webpackJsonp.../../../../rxjs/operators/catchError.js.CatchOperator.call (catchError.js:79)
    at Observable.webpackJsonp.../../../../rxjs/Observable.js.Observable.subscribe (Observable.js:157)
    at MapOperator.webpackJsonp.../../../../rxjs/operators/map.js.MapOperator.call (map.js:56)
    at Observable.webpackJsonp.../../../../rxjs/Observable.js.Observable.subscribe (Observable.js:157)  

【问题讨论】:

    标签: json angular5


    【解决方案1】:

    试试这个

    在传递给 Http POST 之前,您需要对数据进行 JSON.stringify 处理

    JSON.stringify(data)
    

    功能齐全

    editProfileData(data, branchId) {
            console.log('request data', data);
            return this.http.post(this.config.STORE_KEY + '/public/api/business/changerequest/' + 
                     this.userDetails.roleid + '/' + branchId, JSON.stringify(data))
                     .map((response: Response) => response.json())
                     .catch(this.handleError);
        }
    

    我希望这能解决您的问题。如果您有任何问题,请告诉我。

    【讨论】:

    • 谢谢,@Suvethan Nantha
    【解决方案2】:

    这是我的错 我写 时区:this.profileEditForm.controls.timeZone,

    而不是 时区:this.profileEditForm.controls.timeZone.get('zone').value

    所以时区值作为表单组发送

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2018-06-08
      • 2013-06-27
      • 2017-03-22
      • 2021-10-23
      相关资源
      最近更新 更多