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

时间:2013-03-27 11:59:05

标签: php javascript ajax jquery

我正在使用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"));

?>

5 个答案:

答案 0 :(得分:3)

还有一点还没有其他人介绍过:你需要在这里使用双引号或连接:

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

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

另外请在$channel上使用某种验证,因为你的内容非常容易受到SQL注入攻击。

答案 1 :(得分:0)

再次!!!无论如何让我解释一下......

首先你要在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看起来像一个tbalename,如果它是OVERALL那么你可以在你的ajax中传递字符串,如果没有那么你必须在ajax中为channel指定一个tablename

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

答案 2 :(得分:0)

因为您在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'];
数据库选择后

mysql_select_db($dbname);

答案 3 :(得分:0)

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_*函数已被弃用,将来将删除扩展名。不要依赖他们。我用CAPITAL CASE来强调我的观点。

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

Here's关于如何编写保护代码免受SQL注入攻击的代码的帖子。

3)将您的数据库配置详细信息转移到单独的config.php,这样您就不必在每个文件中输入相同的代码,您需要放入SQL查询。

答案 4 :(得分:0)

请试试这个。 1.使用输出缓冲,以便只有json数据将在ajax响应中接收 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 Code

   //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;