当我从表单进行 AJAX 调用时,它显示它是成功的,但数据对象为空。代码如下:
$.ajax(
{
url: 'shipprocess.php',
dataType: 'json',
type: 'POST',
success: function(data)
{
alert('Data is: ' + data);
alert('The AJAX request was a success.');
},
'error': function()
{
alert('An unexpected error occurred.');
}
});
return false;
表格如下所示:
<div class="processedDate">
<form action="shipprocess.php" method="POST" id="shipProcess2" name="shipProcess2">
<input type="hidden" name="empID" value="1" />
<input type="hidden" name="thisOrderID" id="thisOrderID2" value="2" />
<label>Date Shipped </label>
<input type="text" name="shipDate" id="shipDate2" class="shipDate" size="20" value="" />
<input type="submit" name="shipped" id="shipped2" class="shipped" value="Shipped!" />
</form>
</div>
在进行 AJAX 调用后,Firebug 显示状态为 200,内容长度为 0。处理表单的脚本称为 shipprocess.php。返回时会回显以下数据:false;行被注释掉:
[ { "sd": "2012-09-17", "eid": "1", "oid": "2", "efn": "Johnathan", "eln": "Smith" } ]
出于某种原因,脚本不断提醒数据为空。一个完整的例子可以在http://www.yellowcas.com/ship/shipexample.php找到。此示例显示当您提交表单时数据对象为空的警报消息。我还有一个 shipprocess.php 脚本在http://www.yellowcas.com/ship/shipexample1.php返回的数据的完整示例。我以前使用 AJAX 来填充用户输入的邮政编码的城市和州输入字段。jQuery 脚本几乎相同,只是我使用 GET 而不是 POST 作为邮政编码表单。
我曾尝试将 PHP 中的标头声明为 JSON 数据,但这也无济于事。Firebug 似乎也没有给我任何有用的信息。我已经使用另一个名为 testjson.html 的文件测试了脚本。在该文件中,我将有效的 JSON 数据作为文件中唯一没有标题的行,并将数据变量作为对象返回。该示例位于 www.yellowcas.com/ship/shipexample2.php。我不能发布超过 2 个超链接。如果您想查看 shipprocess.php 的代码,我很乐意发布它。我只是不想让这篇文章太长。任何想法将不胜感激。谢谢你。
我决定发布 shipprocess.php 代码,以确保您可以看到我所做的。它如下:
<?php
require_once('dblogin.php');
require_once('dbconnect.php');
require_once('funcs.php');
$err = array();
$datePattern = "!^(\\d\\d)[-/](\\d\\d)[-/](\\d\\d(?:\\d\\d)?)$!";
$psErr = "Shipping date is required.";
$emErr = "Employee ID is missing.";
$orErr = "Order ID is missing.";
if(isset($_POST['shipped']))
{
$postEID = clean($_POST['empID'],$emErr,$n);
$postOID = clean($_POST['thisOrderID'],$orErr,$n);
$postShipDate = clean($_POST['shipDate'],$psErr,$n);
$now = date("Y-m-d H:i:s");
if($postEID == $emErr)
{
$err[] = $postEID;
}
else
{
$query = "SELECT FK_UserID,FirstName,LastName FROM employees WHERE EmployeeID = '$postEID'";
$res = mysql_query($query);
if(mysql_num_rows($res) < 1)
{
$err[] = "Employee does not exist.";
}
else
{
while($row = mysql_fetch_assoc($res))
{
$retUserID = $row['FK_UserID'];
$retFirstName = $row['FirstName'];
$retLastName = $row['LastName'];
}
}
}
if($postOID == $orErr)
{
$err[] = $postOID;
}
if($postShipDate == $psErr)
{
$err[] = $postShipDate;
}
else
{
if (preg_match($datePattern,$postShipDate,$sMatches))
{
$sMonth = $sMatches[1];
$sDay = $sMatches[2];
$sYear = $sMatches[3];
if(checkdate($sMonth,$sDay,$sYear))
{
$shipDate = "$sYear-$sMonth-$sDay";
}
else
{
$err[] = "Invalid shipping date.";
}
}
else
{
$err[] = "Invalid Shipping Date";
}
}
if(empty($err))
// Keep processing the information if there are no errors.
{
$data[] = "$postEID,$shipDate,$postOID,$now,$retFirstName,$retLastName";
}
else
// Return the errors to the user so corrections can be made.
{
$data[] = implode(",",$err);
}
for ($i=0;$i<sizeof($data);$i++)
{
$info = explode(",",$data[$i]);
$data[$i] = $info;
}
$result = array();
for ($y=0;$y<sizeof($data);$y++)
{
if (($data[$y][0]) !== false)
{
array_push($result, array("sd"=>$data[$y][1], "eid"=>$data[$y][0], "oid" => $data[$y][2], "efn"=>$data[$y][4], "eln"=>$data[$y][5]));
}
if (count($result) > 2)
{
break;
}
}
}
echo array_to_json($result);
?>
请尝试我提供的三个示例页面,看看有什么不同的结果。谢谢你。