-1

i have problems calling to my services , i need to call to API with method Basic + Hash for Token. and after i need to send in all services and add the token response.

I have more services that consume from localhost:8080 post and get is fine, this services is ready in server of QA and Developer , but the services token is in a service external.

My Service:

 import 'rxjs/add/operator/map';
 import { Observable } from 'rxjs/Rx';
 import { Injectable } from '@angular/core';
 import { environment } from '../../environments/environment';
 import { Http, RequestOptions, Headers } from '@angular/http';

 const newLocal = JSON.stringify({ 'grant_type': 'password', 'username': 
  'user', 'password': 123456 });

 @Injectable()
 export class Service {

 //Token Api

 private urlTokenApi = environment.urlTokenApi;
 getPrueba(): Observable<any>{
  let headers = new Headers();
  headers.append('Authorization', 'Basic hash');
  headers.append('Content-Type', 'application/x-www-form-urlencoded');
  headers.append('Access-Control-Allow-Origin', '*');
  let options = new RequestOptions({ headers: headers });
  return this.http.post(this.urlTokenApi, newLocal , options )
  .map(
  (response) => {
    return response;
   }
  ).catch((e: any) => 
     Observable.throw(ServiceErrorsManagement.errorHandler(e)))
   .finally(() => {
      this.onFinally();
    });
 }
}

My App:

import 'rxjs/add/operator/map';
import { Component, OnInit } from '@angular/core';
import { environment } from '../environments/environment';
import { HttpClient} from '@angular/common/http'
import { Service } from './services/app.services';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers:[Service]
})

export class AppComponent implements OnInit {

  /* Declaración variables */
  public plamedLogo: String;
  public sanitasLogo: string;
  public sanitasFooter: string;
  private settingsParam = environment.settingsParam;


  /**
   *  Constructor del componente AppComponent.
   */
  constructor(private services : Service) {}

  /**
   * Implementa interface para inicializar valores en las variables.
   */
  ngOnInit(): void {

    this.services.getPrueba().subscribe(
      (response) =>{
        console.log(response);
      },
      (error) =>{
        console.log(error);
      }
    );
  }
}

In the navigator said me:

He said Status 200

enter image description here

In consola:

enter image description here

i need help! :( .

Elgoryboly
  • 43
  • 1
  • 4
  • 3
    Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – Amit Chigadani Jul 09 '18 at 18:31

1 Answers1

1

It seems like CORS problem while you are developing on localhost.

You or your tester can download an allow CORS chrome plugin here and turn it up.

The CORS error should be solved.

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Tewei Lai
  • 46
  • 1