AJAX显示检索到的值为未定义
AJAX showing retrieved values as undefined
我使用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;
- 敲除jsobservable显示未定义的值
- 得到"未定义不是函数“;使用显示原型图案时出错
- Highcharts工具提示不显示corrosconding文本值,而是显示'未定义'
- 使用eval()定义变量显示未定义的错误
- Meteor js-控制台显示'未定义'即使返回结果
- wysihtml5命令显示为未定义
- Json显示来自网站的数据显示仅未定义
- 为什么“;未定义的“;以棱角分明的js显示
- 打字稿显示不能设置未定义的属性,如何解决
- 对象的属性显示为未定义
- JS方法显示了未定义的简单示例
- 当使用多个false时,Select2显示未定义
- AngularJS ng repeat显示原型函数未定义
- 函数在控制台中显示“未定义”
- HTML 对象在使用 ContentPlaceHolder 时在 Javascript 中显示为未定义
- 当我编写脚本时,它显示引用错误:未定义宽度
- 对象属性在控制台.log中显示为未定义
- 当我编写脚本时,它显示类型错误:menuLi[i] 未定义
- 未定义显示在选项中
- 引用错误:未定义显示