AJAX显示检索到的值为未定义

AJAX showing retrieved values as undefined

本文关键字:未定义 显示 检索 AJAX      更新时间:2023-09-26

我使用AJAX向PHP发送值并从PHP检索值。问题是我从PHP获得的值在AJAX中被视为未定义的。请帮我解决这个问题。

AJAX代码:

var channel;
function overall() {
    $(".one").show();
    $(".two").hide();
    $(".three").hide();
    $(".four").hide();
    window['channel'] = "OVERALL";
    $.ajax({
        type: "GET",
        url: "dash2.php",
        data: ({channel: channel}),
        success: function (data) {
            console.log(data.a);
            console.log(data.b);
            console.log(data.c);
        }
    });
}
PHP代码:

<?php
$channel=$_GET['channel'];
$host="192.168.0.29";
$username="root";
$password="root";
$dbname="realcl";
mysql_connect($host,$username,$password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'select * from '.$channel;
$masterresult = mysql_query($query);
while($row1 = mysql_fetch_array($masterresult))
{
    $success=$row1[1];
    $timeout=$row1[2];
    $fail=$row1[3]; 
}
echo json_encode(array("a"=>"$success","b"=>"$timeout","c"=>"$fail"));
?>

这是别人还没有提到的一点:你需要在这里使用双引号或连接:

'select * from $channel'; // no
"select * from $channel"; // yes
'select * from '.$channel; // yes

变量不会在单引号内解析,所以您试图从字面上称为$channel的表中进行选择。

请在$channel上使用某种验证,因为您所拥有的非常容易受到SQL注入攻击。

再次!!不管怎样,让我解释一下……

首先在ajax中使用get方法发送通道…

data:({channel:channel}),  //here which is just a vairable global variable 
                           //and not assigned any value in your given code... 

所以这个发送通道为空…因此,您的查询将无法工作,因为它变成了。

$query = "select * from '' "; <-- here you have a singe quote `'` which also gives error

第二. .Ajax有类型属性,没有方法。

 type:"GET" //here type is get not method

查看你的PHP和查询..$channel看起来像一个表名,如果它是OVERALL,那么你可以把字符串传递到ajax中如果不是,那么你必须给ajax中的channel分配一个表名

 type:"GET" //here type is get not method
 data:{channel:"OVERALL"}, //note you don't need an extra bracket here `()`

因为你正在用json编码你的数据。

尝试在ajax中添加dataType:"json", ajax有type不方法,因此更改为type数据应仅在{}中:

$.ajax({
   type:"GET",
   url:"dash2.php",
   dataType: 'json',
   data:{channel:channel},
   success:function(data){
      console.log(data.a);
      console.log(data.b);
      console.log(data.c);
   }
 });

试着写这行:

$channel=$_GET['channel'];

db选择后:

mysql_select_db($dbname);

设置dataType参数为json

$.ajax({
   type:"GET",
   url:"dash2.php",
   dataType: 'json',
   data:{channel:channel},
   success:function(data){
   }
 });

引用jQuery。ajax文档

dataType定义期望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型来推断。

代码中的一些观察结果。
1)正如上面一些帖子所讨论的,你的代码很容易受到SQL injection attacks的攻击。
2) mysql_*函数已被弃用,扩展将在将来被删除。不要依赖他们。我用大写来强调我的观点。

对于上述两点,尝试使用PDO或MySQLi。PDO或mysql都可以用来保护你的代码免受SQL注入攻击。

这是一篇关于如何编写代码以保护代码免受SQL注入攻击的文章。

3)将您的DB配置细节转移到单独的config.php,这样您就不必在每个文件中放入相同的代码,您想要在

中放入SQL查询。

请尝试一下。1. 使用输出缓冲,以便在ajax响应中只接收json数据2. 在ajax中提供json数据类型

<script type='text/javascript'>
    overall();
    function overall() {
        var channel = 1;
        $.ajax({
            method: "GET",
            url: "http://localhost",
            data: ({channel: channel}),
            dataType: 'json',
            success: function(data) {
                console.log(data.a);
                console.log(data.b);
                console.log(data.c);
            }
        });
    }
</script>
Php代码

   //empty the current contents of the output buffer
    ob_end_clean();
    // Turns on output buffering
    ob_start();
    // print output
    echo json_encode(array("a" => "success", "b" => "timeout", "c" => "fail"));
    // Turn off buffering and print the contents
    ob_end_flush(); // Turn off buffering and print the contents
    exit;