Search code examples
phpjqueryajaxjsonparse-error

Cannot get JSON from Ajax


I am writing an application and I cannot get the JSON using Ajax. Or better, when using XAMPP locally it works correctly, but it doesn't when I upload everything on my domain. What's wrong? PHP seems to work fine, I just get the error function instead of success.

function groupGetDocsList() {
    $.ajax({
        url: '../php/group_docs_list.php',
        dataType: 'json',
        success: function(data) {
            docsList = $.parseJSON(data);
            for(var i = 0; i < docsList.length; i++) {
                docsListNames[i] = docsList[i].replace(/_/g, ' ').replace('.html', '');
            }
            groupSearchDocs();
        },
        error: function(data) {
            $('#group-docs-list-found').append('No documents found.');
            $('#group-docs-list').append('Error loading documents.');
        }
    });
}

Also without $.parseJSON it isn't working.

The console shows this (that is not for my code I suppose since it appears simply including the jQuery javascript):

event.returnValue is deprecated. Please use the standard event.preventDefault() instead.

The status code is 200 and I get a parseerror. I have validated my JSON code with many online validators: they all validate it correctly except for one, but I cannot understand why. It blocks itself on a parenthesis (.

["BMC_Bioinformatics_2008_Oct_1_9_406_ver1.html","BMC_Bioinformatics_2008_Oct_2_9_407_ver1.html","BMC_Cardiovasc_Disord_2002_Mar_27_2_7_ver1.html","BMC_Cardiovasc_Disord_2003_Feb_6_3_2_ver1.html","BMC_Cardiovasc_Disord_2005_Aug_19_5_24_ver1.html","BMC_Cardiovasc_Disord_2005_Aug_25_5_25_ver1.html","BMC_Cardiovasc_Disord_2008_Dec_18_8_39_ver1.html","BMC_Geriatr_2004_Jul_2_4_5_ver1.html","BMC_Geriatr_2004_Oct_15_4_9_ver1.html","BMC_Geriatr_2006_Jan_27_6_3_ver1.html","BMC_Geriatr_2006_Jul_31_6_9_ver1.html","BMC_Geriatr_2007_Dec_19_7_29_ver1.html","BMC_Geriatr_2008_Aug_17_8_19_ver1.html","BMC_Med_Genet_2007_Mar_22_8_13_ver1.html","BMC_Med_Genet_2007_May_30_8_28_ver1.html","BMC_Med_Genet_2009_Jan_30_10_9_ver1.html","BMC_Ophthalmol_2008_Aug_17_8_15_ver1.html","BMC_Pediatr_2003_Sep_4_3_10_ver1.html","BMC_Pediatr_2004_Aug_7_4_15_ver1.html","BMC_Pediatr_2004_Aug_31_4_16_ver1.html","BMC_Pediatr_2004_Dec_14_4_24_ver1.html","BMC_Pediatr_2004_Feb_11_4_3_ver1.html","BMC_Pediatr_2004_Jul_8_4_12_ver1.html","BMC_Pediatr_2004_Sep_1_4_17_ver1.html","BMC_Pediatr_2005_Dec_5_5_45_ver1.html","BMC_Pediatr_2005_Jun_28_5_19_ver1.html","BMC_Pediatr_2005_Nov_9_5_40_ver1.html","BMC_Pediatr_2005_Sep_21_5_38_ver1.html","Breast_Cancer_Res_2005_Jul_27_7(**5)_R775-R779_ver1.html"**,
"Breast_Cancer_Res_2005_Jun_16_7(5)_R669-R680_ver1.html"
**]**

The 2 pieces of code between ** and ** at the end of the JSON is where the validator fails and returns the following errors:

Error:Expecting object or array, not number.[Code 3, Structure 2]
Error:Strings should be wrapped in double quotes.[Code 17, Structure 3]
Error:Expecting ), after ].[Code 2, Structure 6]
Error:Extra closing ][Code 14, Structure 6]

It is sufficient to delete the ( before the 5 in the penultimate string and the JSON is correctly validated.

I have checked many times the URL path, it's correct. The PHP code is this:

<?php
    header('Content-Type: application/json');
    $fileToString = file_get_contents('../documents/');
    $stringToArray = preg_match_all('/[(A-Za-z0-9_\-)]+.html(?=<)/', $fileToString, $matches, PREG_PATTERN_ORDER);
    $jsonDic = json_encode($matches[0]);
    echo $jsonDic;
?>

If opening this file, it correctly prints the json.


Solution

  • First, check your URL path. It's a red flag to me that there is .. at the beginning. Next, as the other commenters said, use the browser console to see any errors or if the request was made successfully.