ajax - PHP: json_encode() SyntaxError if use of include_once() -
remark: using xampp on php i'm using json_encode()
return data js/jquery ajax request.
the ajax call .js:
$.ajax({url: "/sys/search_rf/functions.php" , async: false, // wait reponse type: "post", data: { id: $('#id').val(), status: $('#status').val(), token: ctoken } , success: function(jsonresponse,status){ // post success console.log(jsonresponse); jsondata = json.parse(jsonresponse); [...] }
it works fine until require_once()
.php in functions.php.
require_once(dirname(dirname(__file__)).'/searches.php'); // -> ../searches.php
output debug:
none of included php echoes/print @ all, except function returns json_encode()
.
so then, edited searches.php , left (debugging purpose):
<?php ?>
so i'm 100% sure nothing echoing, , kept:
require_once(dirname(dirname(__file__)).'/searches.php'); // -> ../searches.php
in functions.php, responds ajax request.
but error stills:
syntaxerror: json.parse: unexpected character
and if comment it, there no errors!
// require_once(dirname(dirname(__file__)).'/searches.php'); // -> ../searches.php
i suppose every time require_once()
'echoes' something. don't know.
i checkd eol/encoding of files. in unix format encoded in utf-8.
this workaround do, think it's not proper solution:
jsondata = json.parse( jsonresponse.trim() );
.trim()
you should remove closing php tag , spaces before opening php tag. check if use utf-8 without bom in file.
Comments
Post a Comment