将ajax转换为json

时间:2018-10-25 06:34:36

标签: javascript php json

我试图用json转换我的ajax代码,因为我想用这种方式。 谁能帮我这个忙,因为我不知道该怎么做?具体来说,我希望我的PHP代码将site_id(是一个数字)返回给我的javascript函数,以便以后对其进行操作。.

这是我的JavaScript代码:

function load3() {
    var flag1 = true;
    do {
        var selection = window.prompt("Give the User Id:", "Type a number!");
        if (/^[0-9]+$/.test(selection)) {
            flag1 = false;
        }
    } while (flag1 != false);
    $("#user_id").val(selection)

    var flag2 = true;
    do {
        var selection2 = window.prompt("Give the Book Id:", "Type a number!");
        if (/^[0-9]+$/.test(selection2)) {
            flag2 = false;
        }
    } while (flag2 != false);
    $("#book_id").val(selection2)

    var flag3 = true;
    do {
        var selection3 = window.prompt("Give the Game Id:", "Type a number!");
        if (/^[0-9]+$/.test(selection3)) {
            flag3 = false;
        }
    } while (flag3 != false);
    $("#game_id").val(selection3)

    $.ajax({
        type: 'POST',
        url: 'http://127.0.0.1/PHP/mine1.php',
        data: $('#LoadGame').serialize(),
        success: function(html) {
            //do something on success?
            $('#outPut').html(html);
            var bingoValue = 4;
            if ($('#outPut').text().indexOf('' + bingoValue) > 0) {
                //alert('bingo!');
                window.location.href = 'https://support.wwf.org.uk/';
                //document.location.replace('https://developer.mozilla.org/en-US/docs/Web/API/Location.reload');
            } else {
                alert('No!');
            }
        }
    });
}

这是我想用json制作的PHP代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("127.0.0.1", "root", "", "mysql3");
// Check connection
if($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$user_id =$_POST['user_id'];
$book_id =$_POST['book_id'];
$game_id =$_POST['game_id'];
$site_id =$_POST['site_id'];

header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    $query = "
        SELECT site_id 
        FROM components 
        WHERE user_id='$user_id' 
        AND book_id='$book_id' 
        AND game_id='$game_id' 
        ORDER BY site_id 
        DESC LIMIT 1;
    ";
    $res = mysqli_query($link,$query);
    $result = array();
    $res = mysqli_query($link,$query) or die(mysqli_error($link));
    while($row = mysqli_fetch_assoc($res)){
        $result[] = $row['site_id'];
    }
    echo json_encode($result);

// Close connection
mysqli_close($link);
?>

1 个答案:

答案 0 :(得分:2)

尝试这个:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    $query = "
        SELECT 
            site_id 
        FROM components 
        WHERE user_id = $user_id 
        AND book_id = $book_id
        AND game_id = $game_id 
        ORDER BY site_id DESC 
        LIMIT 1;
    ";
    $res = mysqli_query($link,$query);
    $result = array();
    $res = mysqli_query($link,$query) or die(mysqli_error($link));
    while($row = mysqli_fetch_assoc($res)){
        $result[] = $row['site_id'];
    }
    echo json_encode($result);

我建议使用PDO并将此参数绑定到查询中!这样,您将避免潜在的SQL注入问题。