Advertisement:

Author Topic: Parsing JSON - HTML(loading time?) comment issue  (Read 226 times)

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Parsing JSON - HTML(loading time?) comment issue
« on: March 10, 2019, 03:57:26 pm »
Hello,

Any idea on this comment on the bottom of page source: "<!-- 0.030654191970825 seg. -->"? I've seen it on some Osclass sites. Is it Osclass or server related? Does anyone know how to remove it? I want to parse JSON received from AJAX call but that comment gets in the way and give me an error: "Uncaught SyntaxError: Unexpected token < in JSON at position 48".
Sample response: "{"option_1":null,"option_2":null,"msg":"Already voted."}<!-- 0.0044479370117188 seg. -->".
Using JSON header (header('Content-Type: application/json');) is not helping.

PHP code:

Code: [Select]
<?php
function options_vote() {
    if(
Params::getParam('page') == 'options_vote' && Params::getParam('action') == 'vote') {
        
$response = array();
        
$response['option_1'] = null;
        
$response['option_2'] = null;

        
// Voting code...
        // More voting code...

        
$response['msg'] = 'Voted successfully';

        echo 
json_encode($response);
        exit();
    }
}
osc_add_hook('init''options_vote');
?>


JS code:

Code: [Select]
<script>
        function options_vote(option) {
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    var data = JSON.parse(this.responseText);
                    document.querySelector('.options_vote .response p').innerHTML = data.msg;
                }
            };
            xhttp.open('GET', '<?php echo osc_base_url(1); ?>?page=options_vote&action=vote&option='+option, true);
            xhttp.send();
        }
</script>

Response (with/without JSON header):

Code: [Select]
{"option_1":null,"option_2":null,"msg":"Already voted."}<!-- 0.0044479370117188 seg. -->

Regards.

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Parsing JSON - HTML(loading time?) comment issue
« Reply #1 on: March 10, 2019, 04:07:24 pm »
Found a small JS hack that works in my case, but doesn't solve the original issue. It removes everything after "}", including it, adds "}" again and then parses that data.

Code: [Select]
var data = this.responseText;
data = data.split('}')[0];
data = data + '}';
data = JSON.parse(data);

Regards.

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: Parsing JSON - HTML(loading time?) comment issue
« Reply #2 on: March 10, 2019, 05:56:28 pm »
That value is the page load time and is displayed only when the php debug is active.
Your ajax request is not correct, what I mean with this is that you don't use osclass ajax request link. If you use  index.php?page=ajax path you will have no issues.

oc-includes/osclass/core/BaseModel.php

Code: [Select]
function __destruct()
        {
            if( !$this->ajax && OSC_DEBUG ) {
                echo '<!-- ' . $this->getTime() . ' seg. -->';
            }
        }

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Parsing JSON - HTML(loading time?) comment issue
« Reply #3 on: March 10, 2019, 08:32:24 pm »
Thanks!

EDIT: Proper Osclass AJAX request: https://forums.osclass.org/development/using-ajax-call-from-within-theme-without-breaking-osclass-helpers/15/

hy SmaRTeY .
try this :
Code: [Select]
function ajax_SmaRTeY ()
    {
 do stuff
}
osc_add_hook('ajax_SmaRTeY', 'ajax_SmaRTeY');

front end use this:
Code: [Select]
url: "<?php   echo osc_ajax_hook_url('ajax_SmaRTeY'); ?>"

<?php echo osc_ajax_hook_url('ajax_SmaRTeY', array('id' => $id ) )?>
<?php echo osc_ajax_hook_url('ajax_SmaRTeY', array('id' => $id,'smart'=>$smart ) )?>

back end
Code: [Select]
osc_admin_ajax_hook_url('ajax_SmaRTeY')
just keep the ajax_xxxxx

Regards.
« Last Edit: March 10, 2019, 08:49:22 pm by patrickFromCroatia »