0

I'm trying to do a very simple post http request from Angular2 to a PHP file in localhost// and I'm getting CORS error: image.

The PHP code:

<?php

    header("Access-Control-Allow-Origin: http://localhost/4200");
    header("Access-Control-Allow-Credentials: true ");
    header("Content-Type: application/x-www-form-urlencoded");
    header("Access-Control-Allow-Methods: OPTIONS, GET, POST");
    header("Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");

    $post = file_get_contents('php://input');

    echo($post);
?>

Angular2 http post code in the upload component, inside a upload() function:

upload() {

        var headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

        var params = 'jfjbdjskfbsdjkfs';

        this.http.post('http://localhost/uploadform/prueba.php', params , {headers: headers})
        .subscribe(
            data => console.log('Received:' + data),
            err => console.log(err),
            () => console.log('Call Complete')
        );

    }

The original post that I've trying to do is sending a file to the php and uploading that file to some path in my computer, but since I've been having CORS issues I tried with a simple string as "params", and I'm still having that problem...

I had enabled CORS inside the PHP file in different ways but I'm still not getting any positive result. I've been with this for 3 days now, and it is really frustrating.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Beep
  • 1
  • 1

1 Answers1

0

Read the rest of the error message:

The response had HTTP status code 404

Your PHP isn't running because you got the URL wrong.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • But actually the url is correct. I have my php inside C:\xampp\htdocs\uploadform which means that the url is http://localhost/uploadform/prueba.php, If I'm not mistaken. – Beep May 12 '17 at 19:34
  • @Beep — The server says 404 Not Found, so you are mistaken. – Quentin May 12 '17 at 19:50
  • I can assure you that it is in the right location, I mean, I've checked it... Could it be that the location should be another one instead of C:\xampp\htdocs\uploadform? – Beep May 12 '17 at 19:58
  • I don't know how your server is configured. It is reporting 404 Not Found. The URL *probably* should be something else but it is possible that you'd not put the file somewhere that the server will give it any URL at all. – Quentin May 12 '17 at 20:05