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

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?