Angular - API call 404 Not Found with code OK with Postman

Discussão em 'Angular' iniciado por Matteo Lolli, Abril 14, 2025.

  1. Matteo Lolli

    Matteo Lolli Guest

    I'm new here. I apologize in advance if I write nonsense or wrong things, but I've only just started programming. I'm helping a friend renew his site. I made the new site using Angular with VSCode. We're uploading it to cPanel where the old site is currently. I created a new test folder inside public_html and so far everything is working. The problem arises when we try to make a post call

    `addtest(title: string, description: string, price: number) {
    const endpoint = 'https://www.mydomain.it/test/api/ads.php';
    const body = {
    title,
    description,
    price
    };
    console.log('Request body:', body);
    console.log('API URL:', endpoint);
    return this.http.post(endpoint, body).pipe(
    catchError(err => {
    console.error('Error addtest:', err);
    return of({ message: 'Error: ' + err.message });
    })
    );
    }`


    I created the api folder with ads.php inside, the code is below

    `<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, Authorization');
    header('Content-Type: application/json');

    $host = "localhost:3306";
    $db = "tecnoimmobiliare_test_annunci";
    $user = "tecnoimmobiliare_admin";
    $pass = "NPmLQfqO!4Si";

    $conn = new mysqli($host, $user, $pass, $db);
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    $data = json_decode(file_get_contents("php://input"), true);

    $title = $conn->real_escape_string($data['titolo']);
    $description = $conn->real_escape_string($data['descrizione']);
    $price = floatval($data['prezzo']);

    $sql = "INSERT INTO test_ads (title, description, price) VALUES ('$title','$description', $price)";

    if ($conn->query($sql) === TRUE) {
    echo json_encode(["success" => true, "message" => "Ad placed successfully"]);
    } else {
    echo json_encode(["success" => false, "message" => "Error while inserting: " . $conn->error]);
    }

    $conn->close();
    ?>`


    this call points to this file that should add a new ad to the database.

    But when I start it and in the browser console the console.log tells me 404 not found without details and in the network tab the call is not there (no ads.php).

    I tested the call with Postman and everything works correctly. Has this happened to anyone of you? Can you help me?

    I'm trying to add a new ad to the database, I expect to find it but I get 404 not found in the browser console

    Continue reading...

Compartilhe esta Página