Marcus Marcus - 3 months ago 9x
jQuery Question

JSON.parse not running

My JSON.parse line of code is not running properly. Any ideas? On the run of the function 'showconsultationdata', 2 alert boxes should be shown, but my 2nd alert box doesn't run because the running of the program always stops at the JSON.parse line.
Here is my AJAX script.

function showconsultationdata(str) { //face e.g and checkboxes for that date selected.
var xmlhttp;
if (str == "") {
document.getElementById("txtHint2").innerHTML = "";
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
var a = JSON.parse(xmlhttp.responseText);

Here is my BAConsultRecordsAJAX.php file.

$q = $_GET['q'];
$consult="SELECT * FROM Counsel where nric='$_SESSION[nric]' and dateconsulted='$q'";
$consultresult = mysqli_query($dbconn,$consult);
while($row = mysqli_fetch_array($consultresult)) {
$queryResult[] = $row['skincareremarks'];
$queryResult[] = $row['skinconditionremarks'];
$skincareremarks = $queryResult[0];
$skinconditionremarks = $queryResult[1];
echo json_encode(array('first'=>$skincareremarks,'second'=>$skinconditionremarks));

Oh i tried alerting out the xmlhttp.responseText, and this is what i got.
enter image description here
As you can see, in the last line, it shows the response text value correctly. But im not sure why it is showing my javascripts svc


I managed to solve to problem by filtering out what was being shown in my xmlhttp.responseText.

in my BAConsultRecordsAJAX.php file, i did this.

echo "<div id='test1'>";
echo json_encode(array('first'=>$skincareremarks,'second'=>$skinconditionremarks));
echo "</div>";

I gave this output a div called 'test1'.

Then, in my main file's AJAX script, i did this.

var a = JSON.parse($(xmlhttp.responseText).filter('#test1').html());    

So basically, it filters out the rest of the xhtmlhttp.responseText outputs, and selects only the div id='test1'.

Hope this helps those who have this problem too..