我将数据作为数组发送到php并使用echo检查它。好的,它有效。我看到了我的用户名。但是当我添加$ pdo时,我会看到我所有的HTML(我不想看到)。这有什么问题?
这是我的javascript
var appAdmin=angular.module('appLogin',[]);
appAdmin.service('loginService',
function($http) {
this.submitLogin=function(username,password){
return $http({method:'POST',
url: "php/checkLogin.php",
data: {
username: username,
pass: password
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}); //use $http() here.
}
});
appAdmin.controller('LoginCtrl',function($scope,loginService){
$scope.user={};
$scope.submitLogin=function(){
loginService.submitLogin($scope.user.username,$scope.user.password)
.then(function(data,status,config,headers){
debugger;
console.log('Response from server: '+data.data);
},
function(data,status,config,headers){
console.log('Some error occurred!'); //called in case of error
}
)
}
});
这是我的PHP
<?php
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$username = $request->username;
@$password = $request->password;
$pdo = new PDO('mysql:host=localhost;dbname=subscribers', 'tuviak', 'tuvik1002') ;
echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); //this will go back under "data" of angular call.
?>
这是我的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="../css/styleAdmin.css">
</head>
<body ng-app="appLogin" ng-controller="LoginCtrl">
<h3>Login</h3>
<form name="frmLogin" novalidate>
<input type="text" name="username" ng-hide="true">
<input type="text" name="password" ng-hide="true">
<div id="loginBox">
<div class="lineBox">
<span class="label">Username</span>
<input type="text" name="username" autocomplete="off" ng-model="user.username" ng-required="true" ng-minlength="6" ng-maxlength="8" ng-pattern="/[0-9a-z]/i">
<span class="error-message" ng-show="frmLogin.username.$dirty&& frmLogin.username.$error" ng-hide="frmLogin.username.$dirty&& frmLogin.username.$valid">
The Username is Mandatory
</span>
</div>
<div class="lineBox">
<span class="label">Password</span>
<input type="password" autocomplete="off" name="psd" ng-model="user.password" ng-required="true" ng-minlength="6" ng-maxlength="8" ng-pattern="/[0-9a-z]/i">
<span class="error-message" ng-show="frmLogin.psd.$dirty&&frmLogin.psd.$error" ng-hide="frmLogin.psd.$dirty&& frmLogin.psd.$valid">
The password is Mandatory
</span>
</div>
<div class="lineBox">
<input type="button" value="submit" id="submit" ng-disabled="frmLogin.$invalid" ng-click="submitLogin()">
</div>
</div>
</form>
<script src="../lib/angular.js"></script>
<script src="../js/admin.js"></script>
</body>
</html>
感谢您的帮助!
答案 0 :(得分:0)
我不确定你的PHP是如何构建的 - 你是否只显示了它的一小部分摘录,或者它是否与javascript在同一页面上。如果php在同一页面上并且您正在(通过ajax)发布到同一个脚本,那么使用ob_clean()
将清除在它到达您的php之前创建的任何输出缓冲区
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();
/* your php code - with PDO stuff */
echo htmlspecialchars( $username, ENT_QUOTES, 'UTF-8' );
exit();
}
?>
<html>
<head>
<title>Login</title>
<script>/* Your angularjs code */</script>
</head>
<body>
</body>
</html>
答案 1 :(得分:0)
我的问题的解决方案非常简单:返回的html是连接语句中php错误的解释。