-2

I am trying to call a file that is in this path:

/assets/usuarios/initLogin.php

enter image description here

I run:

normograma > ng serve

and this runs on http://localhost:4200/

I get not found 404 error:

enter image description here

    return new Promise((resolve, reject) => {
    var headers;
        headers = new Headers({
        'Content-Type': 'application/json',
        'Authorization':  token
        });        
    const options = new RequestOptions({
        headers: headers
    });
    this.http.post("http://localhost/assets/usuarios/initLogin.php", {}, options)
    .subscribe(response => {
        try {
        response = response.json();
        resolve(response);
        } catch (error) {
        console.log('[api-274]', response);
        reject(response);
        }
    }, fail => {
        try {
        fail = fail.json();
        } catch (error) {
        console.log('[api-108]', fail);
        }
        reject(fail);
    });
    });

Within the php code is the code that connects to a database that is in the cloud. what am I doing wrong?

yavg
  • 2,761
  • 7
  • 45
  • 115

4 Answers4

1

You're not running these particular php Files on a php server and that is the Problem. What you're trying to do is call php code from an angular server that can only parse typescript, html, or css/scss based on your initial ClientApp arguments. You will need to setup a seperate php server and put your php code on there.

I will just mention a quote from @jeff-lambert from this already answered Thread:

AngularJS is completely client side. You can put PHP in your HTML templates, but if you don't configure your webserver to parse HTML files as PHP then your PHP isn't even going to be parsed.

Even if it did, AngularJS caches these templates so it will only be 'run' on the server a single time. This means if the template in question is swapped out, then data changes on the server that the template makes use of and then it is swapped back in again, the updates to the data are not going to be reflected in the template because there's absolutely zero knowledge on Angular's side of these updates occurring.

A good idea like @Jonast92 says in his comment is to try not to mix client-side and server-side concerns and enforce a strict separation between them. Use Angular models in your angular application's templates.

If you're not familiar with how to setup a php server try XAMPP which works well on windows. It's easy to install and use. After the installation of XAMPP you place your php files inside the httpdocs/wwwroot folder of your Xampp and GET/POST/PUT/DELETE from http://localhost:80/assets/usuarios/initLogin.php instead of http://localhost:4200/assets/usuarios/initLogin.php... Then your PHP Code should get executed from the PHP Server.

Community
  • 1
  • 1
Fy Z1K
  • 618
  • 1
  • 7
  • 21
0

You need to add header, like this in the top of your php file,

header("Access-Control-Allow-Origin: *");

Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
Afzal Patel
  • 124
  • 1
  • 6
0

If you are using Xampp then you need to place file under

Xampp/htdocs/assets/usuarios/initLogin.php

If Wampp Then location would be

Wampp/www/assets/usuarios/initLogin.php

Raahul
  • 399
  • 1
  • 3
  • 11
0

See, 404 means resource not found.

Means you are trying to get some thing (resource ) from server and the server is not able to find that resource.

Probable cause :

The path you are trying is wrong, recheck the path again, also check the port, if your server is configured with port 80 then no port needed else you need to add the port with url, here i am seeing u are directly using localhost.

Pranoy Sarkar
  • 1,965
  • 14
  • 31