Php AJAX不返回对象

Php AJAX不返回对象,php,jquery,wordpress,Php,Jquery,Wordpress,我最初为我的帖子创建了一些过滤器,其中我的functions.php文件将返回html,但是有很多html,所以我决定使用JSON将数据传递给jQuery,然后从我的js文件输出html。但我现在的问题是,当我将响应输出到控制台时,我只得到文本 例如,我试图让每一篇文章永久链接,并且在一个大段落中收到以下内容 [{"permalink":"http:\/\/websitename.co.uk\/property\/rumballs-court-bishops-stortford-hertford

我最初为我的帖子创建了一些过滤器,其中我的functions.php文件将返回html,但是有很多html,所以我决定使用JSON将数据传递给jQuery,然后从我的js文件输出html。但我现在的问题是,当我将响应输出到控制台时,我只得到文本

例如,我试图让每一篇文章永久链接,并且在一个大段落中收到以下内容

[{"permalink":"http:\/\/websitename.co.uk\/property\/rumballs-court-bishops-stortford-hertfordshire-cm23-copy-2\/"},

{"permalink":"http:\/\/websitename.co.uk\/property\/high-wych-road-sawbridgeworth-hertfordshire-cm21-copy\/"},

{"permalink":"http:\/\/websitename.co.uk\/property\/grovebury-house-galloway-road-bishops-stortford-hertfordshire-cm23-copy\/"},

{"permalink":"http:\/\/websitename.co.uk\/property\/rumballs-court-bishops-stortford-hertfordshire-cm23\/"}]
难道我不应该收到一个对象而不仅仅是一个段落吗

这是我的密码

functions.php
函数自定义_js_css(){
wp_enqueue_script('all_js',get_template_directory_uri()。/js/all.min.js','jquery',null,true);
wp_enqueue_style('all_css',get_template_directory_uri()。/css/main.min.css');
wp_本地化_脚本('all_js','ajax_url',admin_url('admin-ajax.php');
}
添加动作(“wp_排队_脚本”、“自定义_js_css”);
添加操作(“wp\u ajax\u soldfilter”、“已售出的\u过滤器”);
添加操作('wp\u ajax\u nopriv\u soldfilter','sall\u filter');
函数(U过滤器(){
$posts=数组(
“每页帖子数”=>-1,
“post_type”=>“property”,
'orderby'=>'date',
“元密钥”=>“属性状态”,
“元价值”=>“售出”
);
$posts['meta_query']=数组('relation'=>'AND');
//价格过滤
如果($\u GET['min\u price']&&!空($\u GET['min\u price'])){
$min_price=$_GET['min_price'];
}否则{
$min_价格=0;
}
如果($\u GET['max\u price']&&!空($\u GET['max\u price'])){
$max\u price=$\u GET['max\u price'];
}否则{
$max_价格=10000000;
}
$posts['meta_query'][]=数组(
'key'=>'property\u price',
'类型'=>'数值',
'value'=>数组($min_price,$max_price),
'比较'=>'之间'
);
//床层过滤
如果($_GET['min_beds']&&!空($_GET['min_beds'])){
$min_beds=$_GET['min_beds'];
}否则{
$MINU床位='1';
}
如果($_GET['max_beds']&&!空($_GET['max_beds'])){
$max_beds=$_GET['max_beds'];
}否则{
$max_床位='9+';
}
$posts['meta_query'][]=数组(
“钥匙”=>“卧室”,
“值”=>数组($min_床,$max_床),
'比较'=>'之间'
);
//位置过滤
如果(isset($\u GET['location'])&&&$\u GET['location'])){
$location=$_GET['location'];
$location_val=stripslashes($location);
$posts['meta_query'][]=数组(
'关系'=>'或',
排列(
“钥匙”=>“街道”,
“值”=>$location\u val,
'比较'=>'类似'
),
排列(
“key”=>“town”,
“值”=>$location\u val,
'比较'=>'类似'
),
排列(
“键”=>“县”,
“值”=>$location\u val,
'比较'=>'类似'
),
排列(
'键'=>'邮政编码',
“值”=>$location\u val,
'比较'=>'类似'
)
);                          
}
//属性类型筛选
如果(isset($\u GET['type'])&&&$\u GET['type'])){
$posts['meta_query'][]=数组(
'key'=>'type_of_property',
'value'=>$\u GET['type'],
'比较'=>'中'
);
}
//二次闪光滤波
如果(isset($\u GET['flash\u type'])&&&$\u GET['flash\u type'])){
$posts['meta_query'][]=数组(
“键”=>“可选类别”,
'value'=>$\u GET['flash\u type'],
'比较'=>'中'
);
}
$query=新的WP_查询($posts);?>
">
到

最高价格
当您在php页面上回显json_编码的数组时,它是json对象的字符串表示形式。您需要将字符串转换为对象

你可以通过两种方式做到这一点

在您的js页面上使用:

var data = JSON.parse(response);
console.log(data);
另一种方法是通过声明预期从PHP脚本接收的数据类型来稍微修饰ajax。请参见
dataType:'json'

$.ajax({
  url: ajax_url,
  data: filter.serializeArray(), // form data
  //type:filter.attr('method'), // POST
  dataType: 'json', //<--This should automatically convert the string to json for you. 
  success:function(response){
   // for(var i = 0; i < response.length; i++){
   //  console.log(response[i]);
   // }
   console.log(response);
   // $('#response').html(data);
   // post_count();
   }
  });
$.ajax({
url:ajax\uURL,
数据:filter.serializeArray(),//表单数据
//类型:filter.attr('method'),//POST

数据类型:“json”,//当您在php页面上回显json_编码的数组时,它是json对象的字符串表示形式。您需要将字符串转换为对象

你可以通过两种方式做到这一点

在您的js页面上使用:

var data = JSON.parse(response);
console.log(data);
另一种方法是通过声明预期从PHP脚本接收的数据类型来稍微修饰ajax。请参见
dataType:'json'

$.ajax({
  url: ajax_url,
  data: filter.serializeArray(), // form data
  //type:filter.attr('method'), // POST
  dataType: 'json', //<--This should automatically convert the string to json for you. 
  success:function(response){
   // for(var i = 0; i < response.length; i++){
   //  console.log(response[i]);
   // }
   console.log(response);
   // $('#response').html(data);
   // post_count();
   }
  });
$.ajax({
url:ajax\uURL,
数据:filter.serializeArray(),//表单数据
//类型:filter.attr('method'),//POST

dataType:'json',/您正在获取ajax编码的字符串,在使用之前应该对其进行解析。否则它只是一个sting,.so

jQuery(function($){
$('#filters').submit(function(e){
    e.preventDefault();

    var filter = $('#filters');


    $.ajax({
        url: ajax_url,
        data: filter.serializeArray(), // form data
         type: POST
        success:function(response){
         var  data = JSON.parse(responce);
             for(var i = 0; i < data.length; i++){
                console.log(data[i]);
             }

    });

});
});
jQuery(函数($){
$(“#过滤器”).submit(函数(e){
e、 预防默认值();
变量过滤器=$(“#过滤器”);
$.ajax({
url:ajax\uURL,
数据:filter.serializeArray(),//表单数据
类型:邮政
成功:功能(响应){
var data=JSON.parse(responce);
对于(变量i=0;i

应该可以工作。

您正在获取ajax编码的字符串,在使用之前应该对其进行解析。否则它只是一个刺,.so

jQuery(function($){
$('#filters').submit(function(e){
    e.preventDefault();

    var filter = $('#filters');


    $.ajax({
        url: ajax_url,
        data: filter.serializeArray(), // form data
         type: POST
        success:function(response){
         var  data = JSON.parse(responce);
             for(var i = 0; i < data.length; i++){
                console.log(data[i]);
             }

    });

});
});
jQuery(函数($){
$(“#过滤器”).submit(函数(e){
e、 预防默认值();
变量过滤器=$(“#过滤器”);
$.ajax({
网址:aj