jQuery AJAX获取MySQL数据返回整个index.html [英] jQuery AJAX get MySQL data returns entirety of index.html

查看:373
本文介绍了jQuery AJAX获取MySQL数据返回整个index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Related Thread: jQuery Ajax returns the whole page

上面的线程是相关的,这似乎是一个普遍的问题,但是在我的情况下,该线程的答案并不能完全解决问题.

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.

单击页面上的图像时,将调用jQuery函数,并且该函数中是一个Ajax声明:

When an image on my page is clicked, a jQuery function is called, and in that function is an Ajax declaration:

//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页面记录到了控制台中.

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.

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

A connection has been made to the database and stored in variable $db prior to any image clicks.

这是我的 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.所以尝试这样,

First identification, you mentioned datatype as "json", but your ajax response is not 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);
?>

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

For debug purpose, try to direct run get.php in browser with query string. Ex, http://...../../get.php?ajaxid=sample_value.

这篇关于jQuery AJAX获取MySQL数据返回整个index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆