如何使用angular2中的form-data发送帖子

时间:2017-03-17 10:33:49

标签: angular post

我尝试使用angular2发布数据,但是当我试图发布时,我有400个错误的请求... 在邮递员中进行测试,外行是可以的,我有200并且成功了

I have a 200 and success...

但是,使用angular2

with angular2

我有一个400错误的请求 我做错了什么?谢谢 !

我的代码。 服务调用API:

userAddReview(paramsObj) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json; charset=UTF-8');
    let params = this.util.transformRequest(paramsObj);
    console.log('sending request');
    return this.authHttp.post(this.wpApiURL + '/users-reviews/reviews?' + params, JSON.stringify({}), { headers: headers })
        .map(
            res => {
                let newReview = res.json();
                this.reviews.push(newReview);
                console.log(this.reviews);
                return newReview;
            }
        );
}

发布组件:

submitReview(form) {
    console.log(this.review, form);
    let params = {
        id: this.review.post,
        user_id: this.wp.getCurrentAuthorId(),
        name: this.wp.getCurrentAuthorId(),
        email: this.wp.getCurrentAuthorId(),
        title:  this.review.rating_title,
        description: this.review.rating_comment,
        rating:     this.review.rating_score,
    };
    console.log("Review", params);
    this.review.author = this.wp.getCurrentAuthorId();
    this.wp.userAddReview(params)
            .subscribe(
                data => {
                    this.statusMessage = "Review added successfully!";
                    //clear form
                    form.reset();
                },
                error => {
                    console.log(error._body);
                    this.statusMessage = error._body;
                }
    );

模板:

<form name="reviewForm" #reviewForm="ngForm" novalidate *ngIf="showPanel()">
    <div *ngIf="!reviewText.valid && (reviewText.dirty || reviewText.touched)" class="alert alert-danger padding">review is required</div>

    <div class="padding">{{statusMessage}}</div>
    <ion-input type="text" [(ngModel)]="review.rating_score" #reviewScore="ngModel" name="reviewScore" placeholder="enter your review score..." required></ion-input>
    <ion-input type="text" [(ngModel)]="review.rating_title" #reviewTitle="ngModel" name="reviewTitle" placeholder="enter your review title..." required></ion-input>
    <ion-textarea
        [(ngModel)]="review.rating_comment"
        #reviewText="ngModel"
        name="reviewText"
        type="text"
        rows="2"
        placeholder="enter your review..."
        required
        >
    </ion-textarea>

    <ion-grid>
        <ion-row>
            <ion-col *ngIf="!isEditMode"><button ion-button block (click)="submitReview(reviewForm)" [disabled]="!reviewForm.valid">Add</button></ion-col>
            <ion-col *ngIf="isEditMode"><button ion-button block (click)="updateReview(reviewForm)" [disabled]="!reviewForm.valid">Update</button></ion-col>
            <ion-col width-33><button ion-button block (click)="onCancel()">Cancel</button></ion-col>
        </ion-row>
    </ion-grid>

</form>

<p *ngIf="!showPanel() && auth.authenticated()" (click)="isEditing = true;">Add Review</p>
<p *ngIf="!auth.authenticated()" (click)="reviewFormNotAuthClicked()">Add Review (login required)</p>

3 个答案:

答案 0 :(得分:2)

chris发送params和header,下面是代码

let url= `${this.wpApiURLl}users-reviews/reviews`;
  let params = new URLSearchParams;
  params.append('id', id);
  params.append('user_id', user_id);
 return this.authHttp.post( url,  { headers:headers,  search:params })
.map(
            res => {
                let newReview = res.json();
                this.reviews.push(newReview);
                console.log(this.reviews);
                return newReview;
            }
        );

答案 1 :(得分:1)

从请求和图像中,看起来您通过网址而不是请求正文发送表单数据。 变化:

return this.authHttp.post(this.wpApiURL + '/users-reviews/reviews?' + params, JSON.stringify({}), { headers: headers })

要:

return this.authHttp.post(this.wpApiURL + '/users-reviews/reviews', params, { headers: headers })

请记住将params设置为URLSearchParams对象。 let params = new URLSearchParams()

答案 2 :(得分:1)

@Venkateswaran,没有什么比这更好了:

    let params = new URLSearchParams;
    params.append('id', '5');
    params.append('user_id', '1');
    console.log('sending request');
    return this.authHttp.post(url, { search:params })
        .map(
            res => {
                let newReview = res.json();
                this.reviews.push(newReview);
                console.log(this.reviews);
                return newReview;
            }
        )
        .subscribe(
                data => {
                    this.statusMessage = "Review added successfully!";
                    //clear form
                    form.reset();
                },
                error => {
                    console.log(error._body);
                    this.statusMessage = error._body;
                }
    );
}

请求paylod为空:

{
  "search": {
    "rawParams": "",
    "queryEncoder": {},
    "paramsMap": {}
  }
}