Ionic 6 API REST post 方法标头

问题描述 投票:0回答:1

我正在尝试发出 API REST 请求,但无法连接 API,我尝试了不同的选项,但我可以做到。

这是我的ts:

post(emplead2){
    let data = {
      "LastName": this.LastName
    }
    
    this.proveedor.addStudent(data)
    .subscribe(
      (data)=>{this.empleados = data;},
      (error)=>{console.log(error);}
    )
  }

这是我的服务

addStudent(data): Observable<any> {
    const headers = new HttpHeaders().append('Content-Type','application/json');
    const body = 'hola';

  console.log(body)
  console.log(headers)
    return this.http.post('APIURL',body,{headers: headers});

  } 

这是错误:

从源“http://localhost:8100”访问“MYAPIURL”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态。

rest ionic-framework post cors http-headers
1个回答
0
投票

这里的问题不在于您的离子应用程序或您发送 POST 请求的方式。基本上发生的情况是,出于安全原因,您的 http 请求被 CORS 策略阻止。您可以在此处查看 CORS 政策到底是什么以及它为何阻止您的请求。

修复此问题取决于您构建 API 所用的语言/框架,但您需要做的是允许您的 ionic 应用程序的基本 url (http://localhost:8100) 访问 API 并绕过 CORS 策略。

© www.soinside.com 2019 - 2024. All rights reserved.