通过AJAX将MySQL / PHP转换为jQuery

I'm saving highscores in a mysql database via PHP. I need to send scores back to my app but It's not working.

This is the first time I'm working with PHP and JSON.. so here is my code

PHP:

   <?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);

                }

any help please?

First of all you can remove the header('Content-Type: application/json'); line from your PHP, you simply don't need it.

In your JavaScript you don't need to parse the result using $.parseJSON because by setting the dataType field to JSON you're telling jQuery you expect a JSON string as a result, and so it automatically parses the result for you. Try this:

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