【发布时间】:2014-06-14 22:55:18
【问题描述】:
我正在编写一个应用程序,但无法使用 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。
【问题讨论】:
-
当
dataType设置为json时,不需要使用$.parseJSON。 jQuery 将在内部完成。听起来像是路径问题。在浏览器控制台/开发工具中检查请求本身。可以查看状态、发送的内容和返回的内容(如果有的话) -
你的 PHP 代码在哪里?
-
你看过控制台了吗? (chromes 开发者控制台,或 firefox Firebug 扩展)
-
@user3666575 你收到 404 错误了吗?
-
Parseerror 表示 jQuery 无法解析 JSON。检查您的 php 文件是否包含 BOM 或任何其他导致响应无效 JSON 的奇怪字符。
标签: php jquery ajax json parse-error