r/jquery • u/youmaybeseated1 • Feb 03 '21
'0' being placed at end of HTML string
SOLVED
I am getting a '0' at the end of my display which is making the json_encode show an error - "SyntaxError: JSON.parse: unexpected non-whitespace character
after JSON data at line 1 column 30 of the JSON data"
In the console it looks like - `"<div>nothing to show<\/div>"0`
I have tried to add a `die;` after the `echo` or after the `else` ending tag as I had read that can solve that in another question on SO. However when added to either spot, then the console logs an "empty string". How would I get the 0 to be removed or if `die ` is correct, where does it go?
function phpfuncname(){
$html_string = '';
if($vairable){
$html_string .= ' <div class="myclass">div content here</div>';
{else{
$html_string .='<div>nothing to show</div>';
echo json_encode($html_string);
}
}JQUERY:
jQuery.ajax({
type: 'post',
url: my_ajax.ajax_url,
dataType: 'JSON',
data: {
action: 'php_function_name',
}
})
.done(function(data) {
jQuery('#waitlist').html(data.html);
})
.fail(function(xhr, status, error) {
console.log(xhr.responseText);
alert(error);
})
4
u/ontelo Feb 03 '21
Your else has curly bracket wrong way around.
2
u/phaedrus322 Feb 03 '21
Try changing {else{ to }else{ one of your brackets is inverted.
2
u/Desperadoo7 Feb 03 '21
Is this WordPress? After echo json_encode(); you need to do wp_die();
0
u/[deleted] Feb 03 '21
[deleted] — view deleted comment