【发布时间】:2017-05-30 00:54:17
【问题描述】:
相关话题:jQuery Ajax returns the whole page
上面的线程是相关的,这似乎是一个有点常见的问题,但该线程中的答案对我的情况并没有帮助。
当点击我页面上的图像时,会调用一个 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 文件:
<?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'];
}
?>
【问题讨论】:
-
第一次识别,你提到的数据类型是“json”,但是你的ajax响应不是json。
-
我尝试将其更改为“文本”,但结果是一样的。
-
尝试使用查询字符串在浏览器中直接运行 get.php。例如,
http://...../../get.php?ajaxid=sample_value