2

I am using Angular2 CLI for my frontend framework and using PHP for my backend.

    this.http.post('assets/modify.php', '')
    .subscribe(result => {
        console.log("success post php file");
      }
    );

I want to use post method to run modify.php. However, I got error: POST XXXXX/assets/modify.php 404 (Not Found)

I can use get method to read the PHP with the same URL, it is working fine. But how can I use Post to run the PHP.

modify.php:

<?php

//lode the file
$contents = file_get_contents('button.json');

//Decode the JSON data into a PHP array.
$contentsDecoded = json_decode($contents, true);

//Modify the counter variable.
$contentsDecoded['button1Status'] = "booked";

//Encode the array back into a JSON string.
$json = json_encode($contentsDecoded);

//Save the file.
file_put_contents('button.json', $json);

?>

The folder structure is:

app------ user--------------- user.component.ts(I am runing get or post method here)

assets----button.json modify.php

when I use get method :

Request URL:http://localhost:4200/assets/modify.php Request Method:GET Status Code:304 Not Modified Remote Address:127.0.0.1:4200 Referrer Policy:no-referrer-when-downgrade

when I use post method:

Request URL:http://localhost:4200/assets/modify.php Request Method:POST Status Code:404 Not Found Remote Address:127.0.0.1:4200 Referrer Policy:no-referrer-when-downgrade


**Just for a update from people's help.

I found this and it help me figure out what happened to my scenario:

executing php files in a angular2cli app

SO I am thinking at the development stage, I need to have a web server can run PHP code.Will have a try on the build-in PHP Server.**

Kevin
  • 272
  • 5
  • 21
  • where are your php scripts located? I doubt they are in 'assets' (where things like css should be I suppose). You just need to get the link right. – Jeff Dec 04 '17 at 23:35
  • @jeff yes, php scripts is located at assets. see my updated. the get method can load it, but post method return a 404 error – Kevin Dec 05 '17 at 00:18
  • 1
    Your GET request may be working but I bet it's not executing your PHP script. Instead, it's probably returning the PHP source code. – Phil Dec 05 '17 at 00:28
  • @PHIL you are right. get method is working, but seems like it only return the php source code. So how can I run the PHP code? I know I can use $.ajax({ url : 'assets/modify.php', method : 'post', success : function( ) { alert( "success" ); } }); but what should I do for an angular2 application?It seems like ajax is not working here – Kevin Dec 05 '17 at 00:33
  • following @Phil's comment I suppose you call your angular app via `file://yourpath/somethingelse`. You need to have a webserver running to serve your files vie `http://localhost:aPortMaybe/myApp` – Jeff Dec 05 '17 at 00:50
  • 1
    You'll need an HTTP server that knows what to do with PHP files. For development purposes, you can use the [built-in PHP server](http://php.net/manual/features.commandline.webserver.php) though you may run into CORS issues with separate servers – Phil Dec 05 '17 at 01:37

1 Answers1

0

Try fully qualifying or at least a good relative URL. I'm going to assume assets is at your docroot, if not then adjust that root slash accordingly:

eg:

/assets/modify.php

or:

https://mywebhost/assets/modify.php     

Your example PHP file doesn't seem to care about anything being sent to it, why use the POST method in the ajax, why not just use GET?

DDeMartini
  • 339
  • 1
  • 6
  • Hey, I have very limit knowledge regarding Angular and PHP. Yes , the PHP file doesn't care about anything being sent to it. What I need to do is to run this PHP ? How to use Post method: this.http.post('assets/modify.php', "") .map(res => res.json()) .subscribe(res => { console.log(res); }, err => { console.log(err); }) I still got 404 error not found. But when I use get method. the php is successfully loaded with the same URL – Kevin Dec 04 '17 at 23:51
  • Did you try to fully quality the path / URI you're posting too? this.http.post('/assets/modify.php',""); The 404 means it can't resolve the location of that resource, and the only think I can think of right now is that you need to fully qualify it. – DDeMartini Dec 05 '17 at 15:46