PHP echo,在对象(所需)之前输出完整的html代码(不需要的)

时间:2013-09-22 05:22:40

标签: javascript php

当我回显$ jsonstring时,似乎整个空白的html页面都是用我请求的对象发送的(从PHP到Javascript)。这里有超级新人,所以我不明白为什么。

我的PHP代码:

<?php

//ini_set('display_errors', '1');
//ini_set('error_reporting', E_ALL);

require "localdbcxn.php";

$encoded = file_get_contents('php://input');
$decoded = json_decode($encoded, true);

$email = $decoded['email'];
$password = $decoded['password'];
$identify = mysql_query("SELECT UserId FROM users WHERE UserEmail='$email' AND UserPassword='$password'");
$numrows = mysql_num_rows($identify);
$row = mysql_fetch_assoc($identify);
$userid = $row['UserId'];

$sessionid = mt_rand(1111111111111111,9999999999999999);
$sessionkey = mt_rand(1111111111111111,9999999999999999);
$logindate = date("Y-m-d H:i:s");


$login = "INSERT INTO mobileSession (UserId, SessionId, SessionKey, BeginDate) VALUES('$userid','$sessionid','$sessionkey','$logindate') ON DUPLICATE KEY UPDATE  SessionId='$sessionid', SessionKey='$sessionkey', BeginDate='$logindate' ";

if ($numrows == 1) {
  mysql_query($login);
  $session = array('UserId'=>$userid,'SessionId'=>$sessionid);
  $jsonstring = json_encode($session);
  echo $jsonstring;
}
?>

以下是控制台(日志)显示的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Untitled Document</title>
</head>

<body>
</body>
</html>{"UserId":"33","SessionId":8207219793564744}

从UserID到... 4744是正确的,但任何人都可以帮助我理解为什么html代码被回应?在我有限的经历中,我以前没有看过这个,并且觉得我做错了。

谢谢!

1 个答案:

答案 0 :(得分:0)

您必须在打印json数据之前指定内容类型。在echo set header之前 -

header('Content-type: application/json');