简体   繁体   English

无法发布-AngularJS,PHP(错误404)

[英]Cannot POST - AngularJS, PHP (error 404)

i'm trying to send information to a PHP page, but it returns me a 404 error status. 我正在尝试将信息发送到PHP页面,但它返回了404错误状态。

<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 app.js的一部分,我正在使用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 register.php

<?php 

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

http://puu.sh/m4FVO/3e607fd137.png 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. 我想发送寄存器信息(名称和数字),用PHP分析数据,如果可以,请用MySQL保存它们。 I don't know why it can't find register.php since the path seems correct, maybe an issue with the server ? 我不知道为什么为什么找不到register.php,因为路径似乎正确,也许是服务器出现问题?

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. 您正在$ http中寻址register.php,就好像register.php脚本与基本html文件位于同一目录中一样。 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. 但是,您的文件列表似乎表明您的index.html文件位于根目录中,而register.php文件位于“脚本”子目录中。

Assuming you don't have any special server-side routing logic, you should update the address in the $http call from: 假设您没有任何特殊的服务器端路由逻辑,则应从以下位置更新$ http调用中的地址:

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

to

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



EDIT: Added info based on comments from OP 编辑:根据来自OP的评论添加了信息

Above answer is still correct -- original code was improperly referencing the register.php file residing in "scripts" directory. 以上答案仍然是正确的-原始代码不正确地引用了位于“ scripts”目录中的register.php文件。 Once fixed, another issue exists. 一旦解决,就存在另一个问题。

OP is using "grunt serve"; OP正在使用“咕serve服务”; however, the grunt server doesn't process php files out of the box. 但是,grunt服务器不会开箱即用地处理php文件。 It's a simple server meant to serve static files (html, js, css, etc.) without server side processing/execution (php). 这是一台简单的服务器,无需服务器端处理/执行(php)就可以提供静态文件(html,js,css等)。 Check out this question/answers for discussion on php under grunt: 查看此问题/答案以进行有关grunt的php讨论:

However, consider installing/running a more full featured web server (Apache, IIS, etc.), especially if you're wanting to deploy this at some point. 但是,请考虑安装/运行功能更全的Web服务器(Apache,IIS等),特别是如果您希望在某个时候进行部署。

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 所以我已经安装了WAMP,编辑了C:\\ wamp \\ bin \\ apache \\ apache2.4.9 \\ conf \\ httpd.conf并将c:\\ wamp \\ www更改为我的工作区文件夹,重新启动了wamp,我使用localhost而不是localhost:9000现在,我必须使用绝对路径:scripts / register.php无法正常工作,我必须使用http://localhost/orion/app/scripts/register.php

thanks y'all for helping. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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