0

HTTP GET Request pending until fail

Hi, I'm trying to establish communication between my front and backend, without success. My client requests are forever in pending state until failure. I seemed to not get what's wrong w my code (below). Anyone who can help out? // -------------------- CLIENT SIDE ---------------------- // export class AuthService { private _BASE_URL: string = "http://localhost:3000/api"; constructor(private http: HttpClient) {} public getUsers(): any { return this.http .get(this._BASE_URL + "/users") .subscribe(data => (data ? console.log(data) : console.log("no data"))); } } // -------------------- SERVER SIDE ---------------------- // const express = require("express"); const bodyParser = require("body-parser"); const cors = require("cors"); const db = require("./sql-connection"); const app = express(); app.use(cors); app.use(bodyParser.json()); app.use( bodyParser.urlencoded({ extended: false }) ); app.use((req, res, next) => { res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept" ); res.setHeader( "Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS" ); next(); }); app.get("/api/users", (req, res, next) => { db.query("select * from users", (error, results, fields) => { if (results.length > 0) { return res.status(200).send(results); } else { return res.status(404).send(); } }); }); module.exports = app;

1st Oct 2019, 11:58 AM
Tafadzwa Muteke
Tafadzwa Muteke - avatar
1 Resposta
0
Tafadzwa Muteke you can get status like response.status===200 in client side then you can display success message. Maybe it can be resolved using promise along with resolve and reject.
23rd Jul 2020, 5:51 PM
paperinflames
paperinflames - avatar