2015-12-23 41 views
2

我試圖發送信息到PHP頁面,但它返回了404錯誤狀態。不能POST - AngularJS,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> 

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

<?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 

我想送註冊信息(名字 和一個數字),在PHP中分析數據,如果他們沒問題,用MySQL保存它們。 我不知道爲什麼它找不到register.php,因爲路徑似乎是正確的,可能是服務器的問題?

如果您有任何想法,我將不勝感激, 謝謝。

+0

使用'/register.php'作爲'$ http.post();' –

+0

嘿,我試過了,不起作用:c http://puu.sh/m5Gt5/2366eae01c。png – garwan50

+0

好吧,只需嘗試完整路徑:http://localhost/APPNAME/scripts/register.php 此處APPNAME是您的項目文件夾名稱。我認爲URL存在一些問題。如果它不起作用,則使用應用程序url進行更正。 –

回答

0

您正在尋址$ http中的register.php,就像register.php腳本與您的基本html文件位於同一目錄中一樣。但是,您的文件列表似乎表明您的index.html文件位於根目錄中,而register.php文件位於「scripts」子目錄中。

假設你沒有任何特殊的服務器端的路由邏輯,你應該更新從$ HTTP調用地址:

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

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



編輯:添加信息根據評論從OP

以上答案仍然正確 - 原始代碼不正確地引用駐留在「腳本」目錄中的register.php文件。一旦修復,存在另一個問題。

OP正在使用「grunt serve」;但是,grunt服務器不會處理php文件。這是一個簡單的服務器,旨在服務靜態文件(HTML,JS,CSS等),而無需服務器端處理/執行(PHP)。退房的討論這個問題/答案在PHP的壓迫下呻吟:

然而,考慮安裝/運行更全功能的Web服務器(Apache時,IIS等),特別是如果你想要在某個時候部署這個。

+0

嘿,我試過這個但仍然不起作用,同樣的404狀態,我真的認爲它會工作,任何其他的想法?謝謝! – garwan50

+0

因此,當您在瀏覽器地址欄中手動輸入「http://whatever.com/index.html」時,您會看到角度應用頁面,對不對?當您手動輸入「http://whatever.com/scripts/register.php」到欄中會發生什麼?另外,你如何引用app.js文件(你的index.html文件中應該有一個引用app.js文件的腳本標籤 - 什麼是src屬性等於)? – Mike

+0

嘿,當我輸入「localhost:9000/scripts/register.php」它下載文件register.php。當我輸入http:// localhost:9000/index.html時,它使用正確的模板將我重定向到http:// localhost:9000/index.html#/ main。,我在我的 index.html' <鏈路rel =「stylesheet」href =「styles/main.css」>',我完整的app.js代碼:http://pastebin.com/jeUXunwb,感謝您的幫助! – garwan50

0

所以我安裝了WAMP,編輯C:\ wamp \ bin \ apache \ apache2.4.9 \ conf \ httpd.conf並將c:\ wamp \ www更改爲我的工作區文件夾,重新啓動wamp,我使用localhost和不localhost:9000現在,我必須使用絕對路徑:腳本/ register.php將無法正常工作,我必須使用http://localhost/orion/app/scripts/register.php

謝謝你們都幫助。