Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用angular2中的form-data发送帖子大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试使用angular2发布数据但是当我试图发布时我有400个错误的请求…
在邮递员中进行测试,外行是可以的,我有200并且成功

 

但是,有了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>

解决方法

克里斯发送params和标题,下面是代码

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;
            }
        );

大佬总结

以上是大佬教程为你收集整理的如何使用angular2中的form-data发送帖子全部内容,希望文章能够帮你解决如何使用angular2中的form-data发送帖子所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。