애플리케이션을 작성 중이며 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와 자바 스크립트를 포함하여 표시하기 때문에 나는 가정)이를 보여줍니다
이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이 올바르게 인쇄됩니다.
'dataType'이''json'으로 설정 될 때'$ .parseJSON'을 사용할 필요가 없습니다. jQuery는 내부적으로 그것을 할 것이다. 경로 문제와 비슷합니다. 브라우저 콘솔/dev 도구에서 요청 자체를 검사하십시오. 상태, 전송되는 내용 및 반환되는 내용 (있는 경우) – charlietfl
PHP 코드는 어디에 있습니까? –
콘솔을 보셨습니까? (크롬 개발자 콘솔 또는 firefox Firebug 확장) –