0
I took this project from a tutorial. The porblema is that it does not clarify where I have to put the folder "api" inside the Angular CLI project is giving error and the sql connection does not work. Where do I put this folder to stop giving this error?
Failed to load Resource: net::ERR_CONNECTION_REFUSED
service
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Car } from './car';
@Injectable({
providedIn: 'root'
})
export class CarService {
baseUrl = 'http://localhost/api';
cars: Car[];
constructor(private http: HttpClient) { }
getAll(): Observable<Car[]> {
return this.http.get(`${this.baseUrl}/list`).pipe(
map((res) => {
this.cars = res['data'];
return this.cars;
}),
catchError(this.handleError));
}
}
api/connect.php
<?php
// db credentials
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'db');
// Connect with the database.
function connect()
{
$connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);
if (mysqli_connect_errno($connect)) {
die("Failed to connect:" . mysqli_connect_error());
}
mysqli_set_charset($connect, "utf8");
return $connect;
}
$con = connect();
In fact, no matter where you put it, it would be better for it to stay where it is or even external to the project. If the database connection is failing, then your problem is in the backend. The error
Failed to load resource: net::ERR_CONNECTION_REFUSED
that the loose angle, is only one more of your propagated error. It would be better if you try to run the applicationPHP
, adjust the connection to the database. After that you can make your angular application consume the json of your application. Another way to test json would be by using the browser. So if applicationPHP
rotate ...– Victor Henrique
you will be able to test using the same url in the browser.
– Victor Henrique