I’m trying to send an ajax result as a json but google inspector is showing a parse error
ajax
jQuery(document).ready(function($){
var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
var page = 1; // What page we are on.
var ppp = 3; // Post per page
$("#misha_loadmore").on("click",function(){ // When btn is pressed.
$("#misha_loadmore").attr("disabled",true); // Disable the button, temp.
$.ajax({
type: 'POST',
cache : false,
url: ajaxUrl,
data:{
"action":"more_post_ajax",
"offset": (page * ppp) + 1,
"ppp": ppp},
dataType: 'json',
success: function(data){
page++;
console.log(data);
//$(".post-grid-container").append(posts); // CHANGE THIS!
$("#misha_loadmore").attr("disabled",false);
},
error: function(error) {
console.log('error:', error);
}
});
});
});
PHP
function more_post_ajax(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
//header("Content-Type: text/html");
$args = array(
'post_type' => 'oportunity',
'posts_per_page' => $ppp,
//'offset' => $offset
);
$loop = new WP_Query($args);
if ($loop->have_posts()){
$return = array();
while ($loop->have_posts()) {
$loop->the_post();
$return[]=array(
'points' => get_field('points'),
'start_date' => get_field('start_date', false, false),
'image' => the_post_thumbnail(),
'link' => get_permalink(get_the_ID()),
'title' => the_title()
);
}
wp_send_json($return);
}
}
This is part of the message I receive:
readyState: 4
status: 200
statusText: "parsererror"
responseText: "[{"points":"1450","image":null,"link":"https://test.io/oportunity/headline-2/"},{"points":"1450","image":null,"link":"https://test.io/oportunity/headline-3/"},{"points":"1450","image":null,"link":"https://test.io/oportunity/headline-4/"}]"
2
Answers
I'll let this here, in case it's useful for anyone. I changed the json format to html.
PHP
ajax
can you try this?