jquery json未返回值

jquery json not returning value

本文关键字:返回值 json jquery      更新时间:2023-09-26

我使用jquery使用serialize提交表单,除了输入多个框外,一切都很好。如果我输入1个框,消息框将显示在#BA_addbox中。然而,如果我使用,作为分隔符输入了多个框,那么在firebug中不会显示任何消息,也不会显示任何json选项卡。只是正确的html。我哪里的代码出了问题。

我已经创建了一个数组,并使用foreach和爆炸来分隔值,但没有返回多个值。感谢

更新:正在php脚本中收集变量,如下所示:

php代码

$dept = mysql_real_escape_string($_POST['customerdept']);
$company = mysql_real_escape_string($_POST['BA_customer']);
$address = mysql_real_escape_string($_POST['customeraddress']);
$service = mysql_real_escape_string($_POST['BA_service']);
$box = mysql_real_escape_string($_POST['BA_box']);
$date = DateTime::createFromFormat('d/m/Y', $_POST['BA_destdate']);
$destdate = $date - > format('Y-m-d');
$authorised = mysql_real_escape_string($_POST['BA_authorised']);
$submit = mysql_real_escape_string($_POST['submit']);
$array = explode(",", $_POST['BA_box']);
     if (isset($_POST['submit']))   {
        foreach ($array as $box) {
        //$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
        //$result = runSQL($sql) or die(mysql_error());
        $form=array('dept'=>$dept,
                 'company'=>$company,
                 'address'=>$address,
                 'service'=>$service,
                 'box'=>$box,
                 'destroydate'=>$destdate,
                 'authorised'=>$authorised,
                 'submit'=>$submit);
        $result=json_encode($form);
        echo $result;

   } 
  }

jquery代码

submitHandler: function()   {
                if ($("#BA_boxform").valid() === true)  { 
                var data = $("#BA_boxform").serialize();
                $.post('/domain/admin/requests/boxes/boxesadd.php', data, function(msg) {
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + msg.box + "</b><br /> You may now close this window.");
                $("#BA_boxform").get(0).reset();
                }, 'json');
         } else
         { 
           return; 
         }
        },
        success:    function(msg)   {
                //$("#BA_addbox").html("You have entered a box");
                //$("#BA_boxform").get(0).reset();
        }   

Per@nnnnnn:

如果有多个框,则生成的json看起来像下面的结构。这是无效的json,因此无法可靠地解析。

{
  ...
}{
  ...
}

要解决此问题,必须将数组添加到另一个数组中,然后对父数组进行编码。

$box = mysql_real_escape_string($_POST['BA_box']);
$array = explode(",", $_POST['BA_box']);
$output = Array();
if (isset($_POST['submit']))   {
  foreach ($array as $box) {
    //$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
    //$result = runSQL($sql) or die(mysql_error());
    $form=array('dept'=>$dept,
                'company'=>$company,
                'address'=>$address,
                'service'=>$service,
                'box'=>$box,
                'destroydate'=>$destdate,
                'authorised'=>$authorised,
                'submit'=>$submit);
    //Add to a parent array instead
    $output[] = $form;
  }
  //encode the entire array
  $result = json_encode( $output );
  echo $result;
}

这将产生以下结构,对于包含解析json的变量data,每个框都可以通过data[0]data[1]等检索。

[
  {
    ...
  },
  {
    ...
  }
]

首先修复php以返回有效的json数组。

if (isset($_POST['submit']))   {
  foreach ($array as $box) {
    //$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
    //$result = runSQL($sql) or die(mysql_error());
    $form=array('dept'=>$dept,
             'company'=>$company,
             'address'=>$address,
             'service'=>$service,
             'box'=>$box,
             'destroydate'=>$destdate,
             'authorised'=>$authorised,
             'submit'=>$submit);
    $result[]=$form;
  }
  echo json_encode( $result );
}

然后post回调中的msg参数应该是一个结果数组,所以不能只执行msg.box来获得框列表。我建议这样做:

boxes = jQuery.map(msg,function(item){
  return item.box;
}).join(',');

从数组中的每个项提取box属性,并将它们连接到逗号分隔的列表中。然后你可以这样显示这个列表:

$("#BA_addbox").html("You have entered box(es): " + "<b>" + boxes + 
  "</b><br /> You may now close this window.");

考虑到这些变化,你的代码对我来说是有效的。如果你有其他问题,我建议你发布更多的html,尤其是表单结构。您的表单可能没有提交正确的值以从服务器获得有效响应。