douluogu8713 2014-02-18 18:38
浏览 38
已采纳

使用数值jQuery PHP解析JSON

I am building a form that submits via Ajax. If our database updates successfully, the function in the controller returns numerical ids, and I would like to use these ids to change the view based on the result.

I am having difficulty parsing the ids that are returned from the controller. They are returned from the controller here:

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!'));
}

The ids are returned like this: [129818,129819,129820]

Now I want to parse this so that I can change the view based on which ids were submitted. I referred to this question JQuery Parsing JSON array but I am still missing something.

When I submit the form I get error "Cannot read property 'length' of null "

Here is the jQuery function (EDIT: I added dataType: 'json', no change in result):

$(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();
              } 
            }
          });

    });
  });
  • 写回答

1条回答 默认 最新

  • dqq46733 2014-02-18 19:17
    关注

    Try with this, ur var product_ids is not necessary

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码