简体   繁体   English

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

[英]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. 我正在尝试使用angular进行发布请求,并将响应作为index.html的html代码获取。 I am using zurb's foundation for apps. 我正在使用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 is by set as root so it will be displaying a login form 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>

This is my app.js file 这是我的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';
    });
   };

 }]);

 })();

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. 现在,一旦我提交表单,我就可以从表单中正确获取数据,但是由于成功功能运行时控制台中会显示index.html的html代码,因此无法正确发布该数据。请提出解决方案这样我就可以从php文件中获取数据。

<?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在任何html代码开始之前编写您的php代码,并在任何html代码开始之前添加die()

login.php login.php

 <?php
     /*
       php code to login
     */

     die();

 ?>

  <html>
    ....
  </html>

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

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