weixin_33682719 2016-05-31 23:30
浏览 22

jQuery JSON问题

I compiled my data into JSON via php with 2 parameters response and status. Response has the data which PHP gets when AJAX is processed and status is the error code or success command.

Now, when I pass the data to jQuery, it comes up with weird back and forward slashes, although are don't appear if I console.log(data.response) on AJAX. Please help ..

IMAGE OF OUTPUT

OUTPUT

THE JS:

jQuery(document).ready(function($) {
    $('.tax-filter').click( function(event) {

        // Prevent defualt action - opening tag page
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }

        // Get tag slug from title attirbute
        var selecetd_taxonomy = $(this).attr('title');

        $('.tagged-posts').fadeOut();

        data = {
            action: 'filter_posts',
            afp_nonce: afp_vars.afp_nonce,
            taxonomy: selecetd_taxonomy,
        };

        $.ajax({
            type: 'post',
            dataType: 'json',
            url: afp_vars.afp_ajax_url,
            data: data,
            success: function( data, textStatus, XMLHttpRequest ) {

                $('.tagged-posts').html( data.response );
                $('.tagged-posts').fadeIn();
                /*console.log( data );
                console.log( XMLHttpRequest );*/
            },
            error: function( MLHttpRequest, textStatus, errorThrown ) {
                /*console.log( MLHttpRequest );
                console.log( textStatus );
                console.log( errorThrown );*/
                $('.tagged-posts').html( 'No posts found' );
                $('.tagged-posts').fadeIn();
            }
        })

    });
});

THE PHP (WP)

// Script for getting posts
function ajax_filter_get_posts( $taxonomy ) {

  // Verify nonce
  if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
    die('Permission denied');

  $taxonomy = $_POST['taxonomy'];

    wp_reset_postdata();
    // WP Query
    $args = array(
      'post_type' => 'std9_photographs',
      'posts_per_page'  => -1,
      'tax_query' => array(
              array(
                'taxonomy' => 'std9_photograph_cat',
                'field'    => 'slug',
                'terms'    => $taxonomy,
              ),
            ),
      );

    // If taxonomy is not set, remove key from array and get all posts
    if( !$taxonomy ) {
      unset( $args['tag'] );
    }

    $query = new WP_Query( $args );

    $result = '';
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

      $result['response'][] = '<h2><a href="'.get_permalink().'">'. get_the_title().' </a></h2>';
      $result['status']     = 'success';

    endwhile; else:
      $result['response'] = '<h2>No posts found</h2>';
      $result['status']   = '404';
    endif;

    $result = json_encode($result);
    echo $result;

    die();
}

add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
  • 写回答

2条回答 默认 最新

  • 关注

    JSON will add slashes when needed (so don't worry about that). The problem is that jQuery has problem to parse that data. You are probably facing a problem encoding your response.

    Edit: In order to avoid your backslashes try encoding with the following command json_encode($str, JSON_UNESCAPED_SLASHES);

    The reason that you are having this forward and back slashes is because you are adding in your response html code with </h2> </a> etc.

    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上