在javascript结果之前有很多空格

时间:2015-01-23 21:11:45

标签: javascript php

我有一个登录脚本,应分别返回'success'或'failure',但它在结果之前添加了许多空格,在控制台中它显示值为“<吨空间>成功”。这是登录脚本的PHP:

public function login() {
    global $dbc, $layout;
    if(!isset($_SESSION['uid'])){
        if(isset($_POST['submit'])){
            $username = mysqli_real_escape_string($dbc, trim($_POST['email']));
            $password = mysqli_real_escape_string($dbc, trim($_POST['password']));
            if(!empty($username) && !empty($password)){
                $query = "SELECT uid, email, username, password, hash FROM users WHERE email = '$username' AND password = SHA('$password') AND activated = '1'";
                $data = mysqli_query($dbc, $query);
                if((mysqli_num_rows($data) === 1)){
                    $row = mysqli_fetch_array($data);
                    $_SESSION['uid'] = $row['uid'];
                    $_SESSION['username'] = $row['username'];
                    $_SERVER['REMOTE_ADDR'] = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : $_SERVER["REMOTE_ADDR"];
                    $ip = $_SERVER['REMOTE_ADDR'];
                    $user = $row['uid'];
                    $query = "UPDATE users SET ip = '$ip' WHERE uid = '$user' ";
                    mysqli_query($dbc, $query);
                    setcookie("ID", $row['uid'], time()+3600*24);
                    setcookie("IP", $ip, time()+3600*24);
                    setcookie("HASH", $row['hash'], time()+3600*24);
                    echo 'success';
                    exit();
                } else {
                    //$error = '<div class="shadowbar">It seems we have run into a problem... Either your username or password are incorrect or you haven\'t activated your account yet.</div>' ;
                    //return $error;
                $err = 'failure';
                echo($err);
                exit();
                }
            } else {
                //$error = '<div class="shadowbar">You must enter both your username AND password.</div>';
                //return $error;
                $err = "{\"result\":\"failure\"}";
                echo json_encode($err);
                exit();
            }
        }
    } else {
        echo '{"result":"success"}';
        exit();
    }
    return $error;
}

和表单和JS

    <div class="shadowbar"><form id="login" method="post" action="/doLogin">
    <div id="alert"></div>
    <fieldset>

     <legend>Log In</legend>
    <div class="input-group">
      <span class="input-group-addon">E-Mail</span>

      <input type="email" class="form-control" name="email" value="" /><br />
    </div>
    <div class="input-group">
      <span class="input-group-addon">Password</span>

      <input type="password" class="form-control" name="password" />
      </div>

    </fieldset>

    <input type="submit"  class="btn btn-primary" value="Log In" name="submit" />

    </form></div>
$(function login() {
    $("#login").validate({ // initialize the plugin
        // any other options,
        onkeyup: false,
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true
            }
        }
    });

    $('form').ajaxForm({
        beforeSend: function() {
            return $("#login").valid();
        },
                success : function(result) {
                    console.log(result);
                    if(result == " success"){
                        window.location = "/index.php";
                    }else if(result == " failure"){
                        $("#alert").html("<div class='alert alert-warning'>Either you're username or password are incorrect, or you've not activated your account.</div>");
                        //$("#alert").show();
                    }
               }
    });
}); 

但由于某种原因,结果总是有很多空格。我是JS的新手,所以如果这很常见,我还不知道。

<?php
    error_reporting(E_ALL); ini_set('display_errors', 1);
    define("CCore", true);
    session_start();
    //Load files...
    require_once('include/scripts/settings.php');
    require_once('include/scripts/version.php');
    require('include/scripts/core.class.php');
    require('include/scripts/nbbc_main.php'); 
    $parser = new BBCode;
    $core = new core;
    $admin = new admin;
    require_once('include/scripts/layout.php');
    require_once('include/scripts/page.php');
    //Set Variables...
    global $dbc, $parser, $layout, $main, $settings, $core;
    $page = new pageGeneration;
    $page->Generate();



?>

这是我的索引,并且在页面生成和调用login()之前的任何内容都在那里。

3 个答案:

答案 0 :(得分:1)

我想你正在使用Ajax调用。我有同样的问题,但我的情况是结果没有包含空格,它在新行中返回。问题是我的脚本是Ajax请求的,在PHP脚本之前包含“新行”字符。在PHP脚本以<?php //code...开头之前搜索脚本文件中的空格如果您在脚本中包含了一些返回成功注释的脚本,那么也要搜索它们。

答案 1 :(得分:0)

我不知道这是否重要,但你的

if(result == " success"){ // <<<<<< Here is a Problem maybe
                    window.location = "/index.php";
                }else if(result == " failure"){ // <<<<<< Here is a Problem maybe
                    $("#alert").html("<div class='alert alert-warning'>Either you're username or password are incorrect, or you've not activated your account.</div>");
                    //$("#alert").show();
                }

将您的服务器结果(即“成功”)与“成功”进行比较。空间太大了。

编辑::我没有得到以太为什么你在响应格式之间跳转。有时你回应“成功”,这对你的if条件很简单,但有时你会返回json编码字符串。 这些回复您不能只与纯文本进行比较。您必须将这些响应解析为JSON对象。然后你可以比较:

if (parsedJSONobject.result == "success"){}

答案 2 :(得分:0)

关于这个问题的评论很可能是正确的:空间正在(再次,可能,没有人可以肯定地知道,而不是阅读整个来源)在此之前包含的PHP回应。例如,如果你这样做:

 <?php
// there's a space before the previous line

你在输出中获得了那个空间。

你可以做的是一些黑客,你包括一个标题,例如:

header('Content-Type: text/html');

在成功输出之前,这将(可能再次)输出类似于:

Warning: Cannot modify header information - headers already sent by (output started at /some/file.php:12) in /some/file.php on line 23

(注意&#34;输出开始&#34;部分)现在你知道从哪里开始寻找。

HTH。