ionic3 - 预检的响应具有无效的HTTP状态代码403

时间:2017-10-19 07:59:53

标签: ionic-framework ionic3

我正在尝试使用post方法,向我的服务器发送一条简单的反馈消息,但在我设置了所有在我的网络浏览器中运行时出现的错误(ionic serve -l

这是在Web浏览器控制台日志中显示的内容 enter image description here

feedback.html

<ion-header>

  <ion-navbar color="danger">
        <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
        <ion-title>反馈</ion-title>
  </ion-navbar>

</ion-header>


<ion-content>
    <div class="logo">
        <img src="assets/img/logo2.jpg">
    </div>

    <form #submitForm="ngForm" novalidate (ngSubmit)="submit(submitForm)">
      <ion-list no-lines>
        <ion-item>
          <ion-label stacked color="danger">请输入您的意见或建议</ion-label>
          <ion-textarea [(ngModel)]="supportMessage" name="supportQuestion" #supportQuestion="ngModel" rows="6" required></ion-textarea>
        </ion-item>
      </ion-list>

      <p ion-text [hidden]="supportQuestion.valid || submitted === false" color="danger" padding-left>
        请勿提交空白信息
      </p>

      <div padding>
        <button ion-button color="danger" block type="submit">提交</button>
      </div>
    </form>
</ion-content>

feedback.ts

submit(form: NgForm) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('api-key', 'MY_API_KEY');

    let body = {
      reason: "This is a message!"
    };

    this.http.post('http://MY_URL/api/user/post_feedback', JSON.stringify(body), {headers: headers})
      .map(res => res.json())
      .subscribe(data => {
          console.log(data);
        });
  }

更新:

在我更改 feedback.ts 文件后,网络浏览器控制台显示不同 enter image description here

所以我认为是api_key问题,但我不知道如何在 feedback.ts 文件中设置我的api-key

enter image description here

feedback.ts

submit(form: NgForm) {

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('api-key', 'MY_API_KEY');

    let body = {
      user_id: 44,
      reason: "This is a message!"
    };

    this.http.post('http://MY_URL/api/user/post_feedback', body, {headers: headers})
      .map(res => res.json())
      .subscribe(data => {
          console.log(data);
        },error => {
          console.log(error);
        });
}

0 个答案:

没有答案
相关问题