简体   繁体   中英

Angular $http.post() returning html code

I am trying to do a post request using angular and getting the response as the html code of index.html. I am using zurb's foundation for apps.

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

is by set as root so it will be displaying a login form 被设置为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>

This is my file 文件

(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';
    });
   };

 }]);

 })();

Now as soon as i submit the form I am able to fetch the data properly from the form but it is not being posted properly since the html code of index.html is being displayed in the console when the success function runs.Please suggest a solution so that i will be able to fetch the data from the php file.

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

and its not working even if I use

file_get_contents("php://input");

In login.php write your php code before any html code starts and add a die() before any html code starts.

login.php

 <?php
     /*
       php code to login
     */

     die();

 ?>

  <html>
    ....
  </html>

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