错误TypeError:将循环结构转换为JSON

时间:2018-04-07 05:50:17

标签: json angular5

我正在使用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)  

2 个答案:

答案 0 :(得分:0)

试试这个

在传递给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);
    }

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

答案 1 :(得分:0)

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

而不是 timezone:this.profileEditForm.controls.timeZone.get('zone')。value

所以时区值以formgroup

的形式发送