我正在编写一个应用程序,但我无法使用Ajax获取JSON。或者更好的是,当在本地使用XAMPP时它可以正常工作,但是当我上传我的域上的所有内容时它不会。怎么了? PHP似乎工作正常,我只是得到错误函数而不是成功。
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.');
}
});
}
也没有$.parseJSON
它无效。
控制台显示了这个(这不是我想要的代码,因为它看起来只是包含了jQuery javascript):
event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
状态代码为200
,我得到parseerror
。
我已经使用许多在线验证器验证了我的JSON代码:它们都正确验证它,除了一个,但我不明白为什么。它在括号(
上阻止自己。
["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"
**]**
JSON末尾的**和**之间的2段代码是验证器失败并返回以下错误:
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]
删除倒数第二个字符串中(
之前的5
并正确验证JSON就足够了。
我已多次检查过URL路径,这是正确的。 PHP代码是这样的:
<?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;
?>
如果打开此文件,则会正确打印json。
答案 0 :(得分:2)
首先,检查您的网址路径。对我来说,这是一个红旗,一开始就有..接下来,正如其他评论者所说,使用浏览器控制台查看任何错误或请求是否成功。