简体   繁体   中英

Cannot POST - AngularJS, PHP (error 404)

i'm trying to send information to a PHP page, but it returns me a 404 error status.

<form name="registerForm" novalidate>
    <div class="field"><label for="name">name : </label><input type="text" ng-model="register.registerInfo.name" name="name" id="name" autocomplete="off" required ></div>
    <div class="field"><label for="number">number : </label><input type="text" ng-model="register.registerInfo.number" name="number" id="number" autocomplete="off" required ></div>
    <br>
    <button ng-click='registerForm.$valid && register.register()'>send</button>
</form>

a part of app.js, i'm using ngrouting

    .controller('RegisterCtrl', ['$http', function($http){

    this.registerInfo = {
        name: '',
        number: ''
    };

    this.register = function() {
        var data = {
            name: this.registerInfo.name,
            number: this.registerInfo.number
        };

        $http.post('register.php', data).then(function(data) {
            console.log(data);
        }, function(error) {
            console.error(error);
        });
    };
}]);

register.php

<?php 

$data = json_decode(file_get_contents('php://input'));
echo 'slt';
echo $data->data->name;

http://puu.sh/m4FVO/3e607fd137.png

│   404.html
│   favicon.ico
│   index.html
│   robots.txt
│
├───images
│       yeoman.png
│
├───scripts
│   │   app.js
│   │   connection.php
│   │   register.php
│   │
│   └───controllers
├───styles
│       main.css
│       style.css
│
└───views
        contact.html
        main.html

I want to send register information (the name and a number), analyze data in PHP and if they are okay save them with MySQL. I don't know why it can't find register.php since the path seems correct, maybe an issue with the server ?

If you have any idea i would appreciate, thanks.

You are addressing register.php in $http as if the register.php script is in the same directory as your base html file. However, your file list seems to indicate that your index.html file is in the root directory, while the the register.php file is in the "scripts" subdirectory.

Assuming you don't have any special server-side routing logic, you should update the address in the $http call from:

$http.post('register.php', data)

to

$http.post('scripts/register.php', data)



EDIT: Added info based on comments from OP

Above answer is still correct -- original code was improperly referencing the register.php file residing in "scripts" directory. Once fixed, another issue exists.

OP is using "grunt serve"; however, the grunt server doesn't process php files out of the box. It's a simple server meant to serve static files (html, js, css, etc.) without server side processing/execution (php). Check out this question/answers for discussion on php under grunt:

However, consider installing/running a more full featured web server (Apache, IIS, etc.), especially if you're wanting to deploy this at some point.

So i've installed WAMP, edited C:\\wamp\\bin\\apache\\apache2.4.9\\conf\\httpd.conf and changed c:\\wamp\\www to my workspace folder, restarted wamp, i use localhost and not localhost:9000 now, and i have to use absolute path: scripts/register.php won't work, i have to use http://localhost/orion/app/scripts/register.php

thanks y'all for helping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM