Javascript 我在echo中看到了我所有的html

Javascript 我在echo中看到了我所有的html,javascript,php,html,angularjs,pdo,Javascript,Php,Html,Angularjs,Pdo,我将数据作为数组发送到php,并使用echo进行检查。好的,它起作用了。我看到了我的用户名。但是当我添加$pdo时,我看到了我所有的html(我不想看到)。这有什么问题 这是我的javascript var appAdmin=angular.module('appLogin',[]); appAdmin.service('loginService', function($http) { this.submitLogin=function(username,password

我将数据作为数组发送到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>

登录
登录
用户名
用户名是必需的
密码
密码是必需的

谢谢你的帮助

我不确定您的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>

登录
/*你的angularjs代码*/

我的问题的解决方案非常简单:返回的html是对连接状态中php错误的解释。

对于变量
postdata
,如果您想获取表单的POST数据,请使用$\u POST[param]。您显示的php是
php/checkLogin.php
中的全部内容?是的。代码来自相关文件bytec0de的第一条注释没有帮助,因为我发送数组并使用angularNo,我的php在单独的页面中。有一个$http调用。问题是我使用angular.js,angular.js发送json而不是值。我是PHP新手,但据我所知,我需要PHP端的json解码器,以便从该json获取值。所以,它是有效的,但当我添加$pdo对象时,我会在echo中看到所有html,而不是值。我想为服务器端和客户端使用完全独立的代码OK。是的,在php代码中,您应该使用json_decode作为正确的状态来解码发布的数据。我从未使用过
angularjs
,但下载并尝试模拟您的代码,但它对我不起作用。。。可能错过了一些东西!我看不出
$pdo
声明会影响事情的原因-在js中,试着调试
submitLogin
,看看它通过POST ajax请求发送了什么。当我再次尝试时,我确实发现控制台中有一个错误-在回调函数中提到
debugger
。我删除了它,但发送的帖子带有空参数。即:没有数据!如果没有$pdo,它就可以工作。我在js文件中看到了回音结果