Angular 8 - httpClient Get method transforms to Options method

风格不统一 提交于 2020-01-06 08:12:19

问题


I am trying to send Get request to my server(Microsoft 2012). I have already added this solution. But in this is solution my get request transformed to Options method.

const httpOptions = {
  headers: new HttpHeaders({
    'Content-Type': 'application/vnd.sas.datamgmt.jobflow.metadata+json; charset=utf-8',
    'Authorization': 'Basic' + btoa('username:password')
  })
};

I also added:

    httpOptions.headers.set('username', 'password');
    httpOptions.headers.append('Access-Control-Allow-Origin', '*');
    httpOptions.headers.append('Access-Control-Allow-Credentials', 'true');
    httpOptions.headers.append('Access-Control-Allow-Methods', 'GET,POST,OPTIONS');
    this.typesOfDBs = this.http.get<Info[]>(this.ROOT_URL, httpOptions);

I watched my request and I saw the request is now Options request. How can I handle it and send request as Get method?


回答1:


Yes right, whenever you calls any API from browser it calls OPTIONS for checking that API really callable or server can handle the request with specified criteria.

If in your case options call gives you success then browser must call actual API with GET method type.

If not calling with GET method type then there could an issues with headers might be sending more or may less.

This type of requests works fine with Postman or fiddler or API testing tools but gives issues with browsers.

I would suggest check for headers first if any interceptor try to comment and then test for this API.

Hope so it would help you.




回答2:


It's just a CORS issue, you need to set the headers you specified on the server side, not on the client side making the request



来源:https://stackoverflow.com/questions/58779934/angular-8-httpclient-get-method-transforms-to-options-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!