简体   繁体   English

jQuery AJAX获取MySQL数据返回整个index.html

[英]jQuery AJAX get MySQL data returns entirety of index.html

Related Thread: jQuery Ajax returns the whole page 相关主题: jQuery Ajax返回整个页面

The above thread is related, and this seems to be a somewhat common problem, but the answer in that thread doesn't exactly help in my situation. 上面的线程是相关的,这似乎是一个普遍的问题,但是在我的情况下,该线程中的答案并不能完全解决问题。

When an image on my page is clicked, a jQuery function is called, and in that function is an Ajax declaration: 单击页面上的图像时,将调用jQuery函数,并且该函数中是一个Ajax声明:

//click an image
$(".img_div").click(function() {

    //get integer stored in alt attribute and pass to variable
    var altCheck = $(this).find('.modal_img').attr('alt');

    //get MySQL data
    $.ajax({

        //php file to grab data
        url: "get.php",
        type: "post",
        datatype: "json",

        //pass above variable to php file
        data:{ ajaxid: altCheck },

        success: function(response){
            //log data to console
            console.log(response);
        },
        error: function(){
            console.log("test");
        }
    });

I'm trying to log the received data into the console purely as a test right now, but I'm getting the entire index.html page logged into the console instead. 我现在正试图纯粹将接收的数据记录到控制台中作为测试,但是我正在将整个index.html页面记录到控制台中。

A connection has been made to the database and stored in variable $db prior to any image clicks. 单击任何图像之前,已与数据库建立连接并将其存储在变量$db

Here is my get.php file: 这是我的get.php文件:

<?php

//variable from jQuery
$value = filter_var($_REQUEST["ajaxid"], FILTER_SANITIZE_STRING);
$value = mysqli_real_escape_string($value);

//variable passed to SQL statement
$sql = $conn->prepare("SELECT FROM table WHERE screeningId = ?");
$sql->bind_param("s",$value);

//Get data
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)){
    //output data
    echo $row['url'];
}
?>

First identification, you mentioned datatype as "json", but your ajax response is not json. 第一次标识时,您提到的数据类型为“ json”,但您的ajax响应不是json。 So Try like this, 所以尝试这样,

<?php

//  Make DB connection variable($conn) available here
//variable from jQuery
$value = filter_var($_REQUEST["ajaxid"], FILTER_SANITIZE_STRING);
$value = mysqli_real_escape_string($conn, $value);

//variable passed to SQL statement
/*$sql = $conn->prepare("SELECT FROM table WHERE screeningId = ?");
$sql->bind_param("s",$value);*/
$sql = "SELECT * FROM table WHERE screeningId = '$value'";
$result = mysqli_query($db, $sql);

//Get data
$result = mysqli_query($db, $sql);
$temp = array();
while ($row = mysqli_fetch_array($result)){
    //output data
    array_push($temp,$row['url']);
}
echo json_encode($temp);
?>

For debug purpose, try to direct run get.php in browser with query string. 为了进行调试,请尝试使用查询字符串在浏览器中直接运行get.php。 Ex, http://...../../get.php?ajaxid=sample_value . 例如, http://...../../get.php?ajaxid = sample_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM