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.

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名