1

REMARK: USING XAMPP On PHP I'm using json_encode() to return data to a JS/JQuery ajax request.

The ajax call from .js:

$.ajax({url: "/sys/search_rf/functions.php" ,
    async: false, // wait for 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 just fine until I require_once() another .php in functions.php.

require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

Output debug: json_encode outputs

None of the included PHP echoes/print anything at all, except the function which returns the json_encode().

So then, I edited searches.php and left it like this (debugging purpose):

<?php

?>

So I'm 100% sure nothing is echoing, and I kept:

require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

in functions.php, which responds to ajax request.

But the error stills:

SyntaxError: JSON.parse: unexpected character

And if I comment it, there will be no errors!

// require_once(dirname(dirname(__FILE__)).'/searches.php'); // -> ../searches.php

I suppose every time I require_once() it 'echoes' something. I don't really know.


I also checkd all EOL/Encoding of all files. All in Unix Format encoded in UTF-8.


This workaround will do, but I think it's not the proper solution:

jsonData = JSON.parse( jsonResponse.trim() );

.trim()

8
  • The error says everything, you're passing some characters to json response. Try removing closing php tags ?> and make sure nothing is printed before the response. You have some weird ??? that are causing this problem, so the json_encode cannot actually encode the string because it's not valid json string. Commented Apr 6, 2014 at 18:16
  • use ob_start/ob_end_clean to be sure that nobody write here) Commented Apr 6, 2014 at 18:16
  • Make sure your searches.php exists and turn off your error reporting. May be you get some errors in including the file or searches.php spits some errors or warnings. Commented Apr 6, 2014 at 18:17
  • ?> removing should solve the issue... There are only pair questionmarks(cr/lf?) in response Commented Apr 6, 2014 at 18:19
  • tried both. ob_start();/ob_end_clean(); and removing the ?>. It ONLY WORKS if I do not require_once(dirname(dirname(__FILE__)).'/searches.php'); even though it has nothing on it. Commented Apr 6, 2014 at 18:25

1 Answer 1

2

You should remove closing php tag and all spaces before opening php tag. Also check if you use UTF-8 without BOM in your file.

Sign up to request clarification or add additional context in comments.

8 Comments

tried both. ob_start();/ob_end_clean(); and removing the ?>. It ONLY WORKS if I do not require_once(dirname(dirname(FILE)).'/searches.php'); even though it has nothing on it.
Can you post somewhere json code,that you get and can't parse and the other you can? pastebin.com would be ok.
It is in the image attached to the post. See, if i copy/paste from debug to notepad they will look exactly the same! But this app JSONedit seems to catch the dirty chars as '??'
I did tried it, But if I don't parse it will be a simple string
Check if you use UTF-8 without BOM in your file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.