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

相关主题: jQuery Ajax返回整个页面

上面的线程是相关的,这似乎是一个常见的问题,但该线程的答案并不完全有助于我的情况。

单击我页面上的图像时,将调用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"); } }); 

我正在尝试将接收到的数据完全作为测试记录到控制台中,但我将整个index.html页面记录到控制台中。

在任何图像点击之前,已与数据库建立连接并存储在变量$db

这是我的get.php文件:

 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']; } ?> 

第一个标识,你提到数据类型为“json”,但你的ajax响应不是json。 所以试试这个,

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

出于调试目的,尝试在浏览器中使用查询字符串直接运行get.php。 例如, http://….. /../ get.php?ajaxid = sample_value 。