Angular $ http.post()返回html代码

时间:2016-01-04 10:40:28

标签: php angularjs zurb-foundation

我正在尝试使用angular进行发布请求并将响应作为index.html的html代码获取。我正在使用zurb的应用程序基础。

<!doctype html>
<html lang="en" ng-app="application">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"   />
<title>Foundation for Apps</title>
<link href="./assets/css/app.css" rel="stylesheet" type="text/css">
<script src="./assets/js/foundation.js"></script>
<script src="./assets/js/templates.js"></script>
<script src="./assets/js/routes.js"></script>
<script src="./assets/js/app.js"></script>
</head>
<body>
<div class="grid-frame vertical">
  <div class="grid-content shrink" style="padding: 0;">
    <ul class="primary condense menu-bar">
      <li><a><strong>opt1</strong></a></li>
      <li><a ui-sref="pros"><strong>opt2</strong></a></li>
    </ul>
  </div>

  <div ui-view class="grid-content" >

    </div>
  </div>
</div>
</body>
</html>

home.html 设置为root,因此它将显示登录表单

<form ng-controller="LoginController as login" ng-submit="login.loginProcess()">
<div class="grid-block">
<div class="grid-content">
    <input type="text" name="username" ng-model="login.user.username">  
</div>  
<div class="grid-content">
    <input type="password" name="password" ng-model="login.user.password">
</div>  
<div class="grid-content">
    <input type="submit" value="submit">
</div>
</div>

</form>

这是我的 app.js 文件

(function() {
'use strict';

var application = angular.module('application', [
'ui.router',
'ngAnimate',

//foundation
'foundation',
'foundation.dynamicRouting',
'foundation.dynamicRouting.animations'
])
.config(config)
.run(run)
;

config.$inject = ['$urlRouterProvider', '$locationProvider'];

function config($urlProvider, $locationProvider) {
$urlProvider.otherwise('/');

$locationProvider.html5Mode({
  enabled:false,
  requireBase: false
});

$locationProvider.hashPrefix('!');
}

function run() {
FastClick.attach(document.body);
};

application.controller('LoginController',['$scope','$http',function($scope,$http){
this.user = {};
this.loginProcess = function(){
  console.log(JSON.stringify(this.user));

   var postData = JSON.stringify(this.user);
   var config = {method: 'POST', url: '/login.php', data:postData};
    $http(config)
   .success(function(data, status, headers, config) {
    console.log(data);
     })
    .error(function(data, status, headers, config) {
    $scope.errorMsg = 'Unable to submit form';
    });
   };

 }]);

 })();

现在,只要我提交表单,我就可以从表单中正确地获取数据但是由于index.html的html代码在成功函数运行时显示在控制台中,因此无法正确发布。请建议一个解决方案,以便我能够从php文件中获取数据。

<?php
echo $_REQUEST['username'];
?>

即使我使用

也无法正常工作
file_get_contents("php://input");

1 个答案:

答案 0 :(得分:0)

login.php之前,在任何html代码开始之前编写您的PHP代码,并在任何html代码开始之前添加die()

<强>的login.php

 <?php
     /*
       php code to login
     */

     die();

 ?>

  <html>
    ....
  </html>