当我从表单进行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。当return: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数据,但这也没有帮助。萤火虫似乎也没有给我任何有用的信息。我已经使用一个名为testjson.html的不同文件测试了脚本。在该文件中,我将有效的json数据作为文件中唯一没有头的行,它将数据变量作为对象返回。该示例位于www.yellowcas.com/ship/shipexample2.php。我最多只能发布两个超链接。如果您想查看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);
?>

请尝试我提供的三个示例页面,看看不同的结果是什么。谢谢您。

最佳答案

你的代码至少缺少两件事:
1:用ajax发帖的时候,需要发送帖子数据。所以你必须知道你想发送什么数据。大多数情况下,它将是序列化的表单数据,但它可以是任何东西。

var dataString = 'name=Gr G';
$.ajax(
  {
    url: 'shipprocess.php',
    dataType: 'json',
    data: dataString,
    type: 'POST',
    ...

2:您希望返回值,但不发送返回值。在处理后的shipprocess.php中,您应该回声如下:
...
echo 'data received and processed';
...

关于php - 调用AJAX之后,数据为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12596900/

10-11 07:55
查看更多