How can I connect my front (localhost) with my back in (localhost)

0

The case is as follows, I have a front made in angular and I already managed to make the connection between the back and the database, all this locally ... the problem is the following, when I try to obtain back information from my front with a service using angular http protocol, it is impossible to get localhost information, however I can get it from a virtual back end.

The portion of the code that is annexed is part of the service, front ..

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class DataService {

  uri = '';

  constructor(private http: HttpClient) { }

  getUsers() {
    return this.http.get('https://jsonplaceholder.typicode.com/users')

  }

  getUser(userId) {
    return this.http.get('https://jsonplaceholder.typicode.com/users/'+userId)
    
  }

  getPosts() {
    return this.http.get('https://jsonplaceholder.typicode.com/posts')

  }

  getPuppies() {
    return this.http.get('http://localhost:3000/api/puppies/')

  }

of this line for example > return this.http.get (' link ')

I can get data without problem

but of this  getPuppies () {     return this.http.get (' link ')

} I can not get any information ...

and the nodejs is synchronized with the database understand that when accessing from the browser of localhost: 3000 I get the json of the database created in postgres ... does anyone have any proposal?

    
asked by Jesus Alfredo 06.09.2018 в 22:14
source

1 answer

0

It seems to be a CORS problem you need your BackEnd server to generate the corresponding 'headers' of the type: 'Access-Control-Allow-Origin: *'.

This is so that requests made from Javascript can have access (permission) to receive the json.

In this link you can find information on what CORS is and why you need it.

    
answered by 07.09.2018 в 01:40