2014-06-14 201 views
-1

我在编写应用程序,无法使用Ajax获取JSON。或者更好的是,当本地使用XAMPP时它可以正常工作,但是当我上传我的域中的所有内容时不会发生。怎么了? PHP似乎工作正常,我只是得到错误功能,而不是成功。无法从Ajax获取JSON

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" 
**]** 

的2片之间的代码**和**在JSON的端部是在验证失败并返回以下错误:

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。

+2

当'dataType'设置为'json'时,不需要使用'$ .parseJSON'。 jQuery将在内部完成。听起来像一个路径问题。在浏览器控制台/开发工具中检查请求本身。可以查看状态,发送的内容和返回的内容(如果有的话) – charlietfl

+0

您的PHP代码在哪里? –

+0

你有看看控制台吗? (chromes开发者控制台或firefox Firebug扩展) –

回答

2

首先,检查你的URL路径。对我来说,这是一个红旗,开头是..接下来,正如其他评论者所说,使用浏览器控制台查看任何错误或请求是否成功。

+1

你是对的!它是URL路径,它需要与调用该函数的页面相关,而不是函数所在的位置。非常感谢你! – TheOldMe

+0

非常好!很高兴我能帮上忙! –