json_decode在print_r上不提供任何数据,但是$ _GET中存在原始数据

时间:2018-08-12 18:33:03

标签: php json

我已经使用AJAX传递了一个数组

htmlspecialchars(json_encode($cInfo))

在我的其他php文件中,如果可以

print_r ($_GET);

我可以看到通过AJAX传递的所有数据,如下所示:

Array ( [action] => edit_category [cPath] => [cID] => 141 [cInfo] => {"categories_id":"141","categories_name":"Mens","categories_description":"Get ready for warmer days with our new season trends for Spring\/Summer. Whether you're after an iconic Superdry jacket, a pair of great fitting jeans or a cool tee, shop our latest collections right here. Discover premium quality outerwear for those changeable spring days and style with the latest T-shirts, shirts and trainers.","categories_image":"categories\/cat-head-1-min.jpg","parent_id":"0","sort_order":"10","date_added":"2016-09-15 23:43:02","last_modified":"2018-02-21 15:52:14","categories_status":"1"} ) 

但是,如果有以下代码:

$cInfo = json_decode($_GET['cInfo']);

,然后尝试print_r($ cInfo)我什么都没收到。我不明白为什么$ _GET

中明显存在数据时无法使用这些数据

下面添加了更详细的代码。

php生成提交json编码数组和其他数据的表单:

    $lc_text .= '<form id="category-edit-'.$categories->fields['categories_id'].'" class="tooltip-category-edit-'.$categories->fields['categories_id'].'" data-tooltip-content="#category-edit-tooltip-content-'.$categories->fields['categories_id'].'">
                     <input type="hidden" name="action" value="edit_category" />
                     <input type="hidden" name="cPath" value="'.$cPath.'" />
                     <input type="hidden" name="cID" value="'.$categories->fields['categories_id'].'" />
                     <input type="hidden" name="cInfo" value="'.htmlspecialchars(json_encode($cInfo)).'" />
                     <input type="image" class="icon-edit" src="images/icon_edit.png" border="0" alt="'.ICON_EDIT.'" title="'.ICON_EDIT.'" />
                 </form>
                 <div class="tooltip_templates">
                     <div id="category-edit-tooltip-content-'.$categories->fields['categories_id'].'">
                         <div class="tooltip-loading-'.$categories->fields['categories_id'].' load-text">Loading...</div>
                         <div class="edit-category-results-'.$categories->fields['categories_id'].'"></div>
                     </div>
                 </div>';                

提交表单的jQuery:

    $(document).ready(function() {
      // Variable to hold request
      var request;
      $('.tooltip-category-edit-<?php echo $categories->fields['categories_id']; ?>').tooltipster({
          plugins: ['sideTip', 'scrollableTip'],
          trigger: 'click',
          interactive: 'true',
          maxWidth: 600,
          contentAsHTML: 'true',
          functionAfter: function() {
            // Add background overlay
            if ($('#overlay-mask').length) {
                $('#overlay-mask').hide();
            }
          },
          functionBefore: function(instance, helper) {
            var $origin = $(helper.origin);

            // Add background overlay
            if (!$('#overlay-mask').length) {
                $('body').append('<div id="overlay-mask" style="display: block;"></div>');
            } else {
                $('#overlay-mask').show();
            }

            if ($origin.data('loaded') !== true) {
                // Abort any pending request
                if (request) {
                    request.abort();
                }

                // setup local variables
                var $form = $('.tooltip-category-edit-<?php echo $categories->fields['categories_id']; ?>');

                // Let's select and cache the fields
                var $inputs = $form.find();

                // Serialize the data in the form
                var serializedData = $form.serialize();

                // Send the request
                request = $.ajax({
                    url: "categories_edit_ajax.php",
                    type: "get",
                    data: serializedData
                });

                // Callback handler on success
                request.done(function (response, textStatus, jqXHR){
                    instance.content(response);
                    $origin.data('loaded', true);
                });
            }
          }
          });

    // Variable to hold request
    var request;

    // Bind to the submit event of our form
    $("#category-edit-<?php echo $categories->fields['categories_id']; ?>").submit(function(event){
        // Prevent default posting of form
        event.preventDefault();
        return false;
    });
    });

AJAX提交到的文件的开始,其中$ _GET显示了我无法进入可用数组的正确数据

<?php
$heading = array();
$contents = array();
$cPath = isset($_GET['cPath']) ? filter_var(trim($_GET['cPath']), FILTER_SANITIZE_STRING) : null;
$cID = isset($_GET['cID']) ? filter_var(trim($_GET['cID']), FILTER_SANITIZE_STRING) : null;
$cInfo = json_decode($_GET['cInfo']);

echo '<div class="debug">';
echo "get content: ";
print_r($_GET);
echo '<br/>';

echo "cPath: ".$cPath.'<br/>';
echo "cID: ".$cID.'<br/>';
echo "cInfo: "; print_r($cInfo).'<br/>';
echo '</div>';


$on_image_delete = false;
$off_image_delete = true;
$heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_EDIT_CATEGORY . '</b>');

$contents[] = array('text' => zen_draw_form('categories', FILENAME_CATEGORIES, 'action=update_category&cPath=' . $cPath . ((isset($_GET['search']) && !empty($_GET['search'])) ? '&search=' . $_GET['search'] : ''), 'post', 'enctype="multipart/form-data"') . zen_draw_hidden_field('categories_id', $cInfo->categories_id));
$contents[] = array('text' => TEXT_EDIT_INTRO);

print_r($ _ GET);

的输出
/Applications/MAMP/htdocs/justsimplecart/jscadmin/categories_edit_ajax.php:22: array(4) { 'action' => string(13) "edit_category" 'cPath' => string(0) "" 'cID' => string(3) "141" 'cInfo' => string(769) "{"categories_id":"141","categories_name":"Mens","categories_description":"Get ready for warmer days with our new season trends for Spring\/Summer. Whether you're after an iconic Superdry jacket, a pair of great fitting jeans or a cool tee, shop our latest collections right here. Discover premium quality outerwear for those changeable spring days and style with the latest T-shirts, shirts and trainers.","categories_image":"categories\"... } 

1 个答案:

答案 0 :(得分:0)

如果解码json时遇到问题,则该函数可能返回null。

如果您想知道问题所在,可以使用函数 json_last_error json_last_error_msg 。。

>