2014-12-15 3 views
0

PHP를 통해 mysql 데이터베이스에 최고 기록을 저장하고 있습니다. 점수를 내 앱으로 보내야하지만 작동하지 않습니다.AJAX를 통해 MySQL/PHP에 jQuery

<?php 
    $server = "..."; 
    $user = "..."; 
    $pass = "..."; 
    $bd = "..."; 

    $conexion = mysqli_connect($server, $user, $pass,$bd) 
    or die("Ha sucedido un error inexperado en la conexion de la base de datos"); 

    $sql = 'SELECT * FROM table_one ORDER BY score DESC LIMIT 10'; 

    if(!$result = mysqli_query($conexion, $sql)) die(); 
    $scores = array(); 
    while($row = mysqli_fetch_array($result)) 
    { 
     $name=$row['name']; 
     $score=$row['score']; 
     $scores[] = array('name'=> $name, 'score'=> $score); 
    } 
    $close = mysqli_close($conexion) 
    or die("Ha sucedido un error inexperado en la desconexion de la base de datos"); 

    $json_string = json_encode($scores); 
    header('Content-Type: application/json'); 
    echo $json_string; 
?> 

JQUERY

$.ajax({ 
       method: 'GET', 
       url: 'score2.php', 
       dataType: 'json', 
       success: function(result) { 
       var data = jQuery.parseJSON(result); 
       console.log(data); 

       } 

도움하십시오

이것은 내가 지금 여기에 ... PHP와 JSON 함께 일하고 있어요 처음으로 내 코드

PHP이다?

+0

당신이 생각하는 부분은 작동하지 않습니다.? – Saif

+0

PHP에서는'header ('Content-Type : application/json');'줄이 필요하지 않으므로 jQuery에서 결과를 구문 분석 할 필요가 없습니다. dataType을 JSON으로 설정하면 jQuery가 자동으로 결과를 파싱합니다. – Styphon

+0

결과의 상태는 어떻습니까? 200 !! 아약스 호출의 응답은 무엇입니까? – Moussawi7

답변

0

먼저 PHP에서 header('Content-Type: application/json'); 행을 제거 할 수 있습니다. 간단히 말해서 필요하지 않습니다.

JavaScript를 사용하면 dataType 필드를 JSON으로 설정하면 jQuery에 결과로 JSON 문자열이 필요하므로 자동으로 결과를 파싱하기 때문에 결과를 $.parseJSON으로 구문 분석 할 필요가 없습니다. 이것을 시도하십시오 :

$.ajax({ 
    method: 'GET', 
    url: 'score2.php', 
    dataType: 'JSON', 
    success: function(data) { 
     console.log(data); 
    } 
}); 
+0

감사합니다.하지만 아무 일도 일어나지 않습니다. –

관련 문제