简体   繁体   English

从Angular应用程序调用Rest API

[英]Call Rest API From Angular Application

I want to call a Rest API From my Angular Front End but i am facing the permission issue of Access control origin 我想从Angular前端调用Rest API,但是我面临访问控制源的权限问题

This is my code of front end 这是我的前端代码

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpHeaders } from '@angular/common/http';
var httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/vnd.onem2m-res+json;ty=4',
    'Accept': 'application/json',
    'X-M2M-RI': '9900001',
    'X-M2M-Origin': 'C7AACE9CB-258b9773',
    'Authorization': 'Basic QzdBQUNFOUNCLTI1OGI5NzczOlRlc3RAMTIz',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,OPTIONS',
    'Access-Control-Allow-Headers': 'Content-Type, Authorization, Content- 
     Length, X-Requested - With'
     }
  )
};

@Injectable()
export class ApiService {
  constructor(private http: HttpClient) { }
  fetch_latest_data(): Observable<any> {
    return this.http.get("http://168.87.87.213:8080/davc/m2m/HPE_IoT/ 0000acfffe6f290b/default/latest", httpOptions);

  }
}

I am making this request from front end that is from angular i am not using any backend such as node or express for this please tell me can i make request this way is it possible or not and if it is possible then please help me to resolve the above issue 我正在从前端发出此请求,该请求是从角度出发的,我未使用任何后端(例如节点或表达),请告诉我是否可以这种方式发出请求,如果可以,那么请帮助我解决上述问题

The cors headers must be enable at Server-side not client side. 必须在服务器端而非客户端启用cors标头。

 'Access-Control-Allow-Origin': '*',
 'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,OPTIONS',
 'Access-Control-Allow-Headers':'Content-Type, Authorization, Content-Length, X-Requested-With'

and if your server-side need credentials you can send it from client by 'withCredentials' in the httpOptions : 并且如果您的服务器端需要凭据,则可以通过httpOptions中的“ withCredentials”从客户端发送它:

var httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/vnd.onem2m-res+json;ty=4',
    'Accept': 'application/json',
    'X-M2M-RI': '9900001',
    'X-M2M-Origin': 'C7AACE9CB-258b9773',
    'Authorization': 'Basic QzdBQUNFOUNCLTI1OGI5NzczOlRlc3RAMTIz',
 }),
 withCredentials: true;
};

Finally the server-side is responsible to define CORS enable or not. 最后,服务器端负责定义是否启用CORS。

If the server-side source is not yours, you have 3 choices. 如果服务器端源不是您的,则有3种选择。

  1. send your client side request with Origin: http://168.87.87.213:8080 at headers parameter. 在标头参数中发送带有Origin的客户端请求: http : //168.87.87.213 : 8080 But I cannot approve this solution. 但是我不能批准这种解决方案。

  2. Deploy your client app in side of your server app deployment with the same ip and port address. 使用相同的ip和端口地址在服务器应用程序部署中部署客户端应用程序。

  3. Make a new project as a mediator to response your specific client side request and deploy it in side of server app deployment. 制作一个新的项目作为中介,以响应您的特定客户端请求,并将其部署在服务器应用程序部署中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM