dthy81285 2014-06-14 22:55
浏览 44
已采纳

无法从Ajax获取JSON [关闭]

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.

  • 写回答

1条回答 默认 最新

  • dongqian6484 2014-06-14 23:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64