【问题标题】:Parse JSON with numerical values jQuery PHP用数值 jQuery PHP 解析 JSON
【发布时间】:2014-02-18 18:38:46
【问题描述】:

我正在构建一个通过 Ajax 提交的表单。如果我们的数据库更新成功,控制器中的函数会返回数字 id,我想使用这些 id 根据结果更改视图。

我无法解析从控制器返回的 ID。它们从这里的控制器返回:

foreach($update_pending as $product){
    $success_ids[] = $product['product_id'];
}

if(isset($success_ids)){
    echo json_encode($success_ids);
}else{
    echo json_encode(array('error'=>'Error!'));
}

ID 如下返回:[129818,129819,129820]

现在我想解析这个,以便我可以根据提交的 id 更改视图。我提到了这个问题JQuery Parsing JSON array,但我仍然缺少一些东西。

当我提交表单时出现错误“无法读取 null 的属性‘长度’”

这里是 jQuery 函数(编辑:我添加了 dataType: 'json',结果没有变化):

$(function () {
    $('form').on('submit', function (event) {
        var product = $(this).attr("data-id");

        event.preventDefault();

          $.ajax({
            type: 'POST',
            url: 'category_typeahead',
            data: $('form').serialize(),
                dataType: 'json',
            success: function (data) {
              if(data.error == undefined){
                alert(data);
                product_ids = $.parseJSON(data);

                for (var i=0, len=product_ids.length; i < len; i++) {
                    $(product_ids[i]).hide();
                }

              }else{
                if($('.error_true').length==0){
                    $('#error').append('<div class="alert alert-error error_true">Error text</div>');   
                }
                $('#error').show();
              } 
            }
          });

    });
  });

【问题讨论】:

  • 请发布console.log(product_ids)的输出
  • 嗨,首先,尝试在你的 ajax dataType: 'json' 后面添加 data: $('form').serialize(),
  • 好的,刚刚试了一下,结果没有改变,console.log(product_ids)返回“null”。 console.log(data) 像这样返回 product_ids:[129826, 129827, 129828]
  • 不确定你为什么要对它进行字符串化。并且您不需要product_ids = $.parseJSON(data);,而是按照之前的建议添加数据类型。
  • 摆脱product_ids = $.parseJSON(data)。它不是必需的,可能会导致问题。

标签: php jquery ajax json


【解决方案1】:

试试这个,你的 var product_ids 不是必需的

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
    $('form').on('submit', function (event) {
        var product = $(this).attr("data-id");

        event.preventDefault();// using this page stop being refreshing 

          $.ajax({
            type: 'POST',
            url: 'category_typeahead',
            data: $('form').serialize(),
                dataType: 'json',
            success: function (data) {
              if(data.error == undefined){
                data.forEach(function(entry){
                    $('#'+entry).hide();
                  });

              }else{
                if($('.error_true').length==0){
                    $('#error').append('<div class="alert alert-error error_true">Error text</div>');   
                }
                $('#error').show();
              } 
            }
          });

    });
  });
</script>

【讨论】:

  • 谢谢!是的,您还注意到我缺少 data.hide() 变量旁边的标识符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2012-05-14
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
相关资源
最近更新 更多