PHP5.1.6 has no json_encode(), so I wanted to use function found on json_encode documentation. I am trying to use output of this function with ajax request:
fetchArticles: function( e ) {
$.ajax({
url: 'article.php',
type: 'POST',
data: { id: $(this).data( 'id_prod' ) },
dataType: 'json',
success: function( results ) {
console.log('finished');
console.log(results);
}
});
In article.php
I have at this time:
if ( isset($_POST['id']) ) {
connect();
$articles = get_articles( $_POST['id'] );
echo json_encode( $articles ); return;
}
The problem is with returning results to JS console:
finished
.I can see in HTTP headers and responses that proper data is returned, but it is not printed to console. Could you help me with solving this problem?
When the json file can't be converted it throws parsererror
exception, so try this:
$.ajax({
url: 'article.php',
type: 'POST',
data: { id: $(this).data( 'id_prod' ) },
dataType: 'json',
success: function( results ) {
console.log('finished');
console.log(results);
},
error: function(jqXHR, textStatus, errorThrown) {
console.debug(jqXHR, textStatus, errorThrown);
}
});
It will output your error when the JSON is invalid.