我想要做的是通过ajax和PHP调用一些数据库数据.但是ajax调用不起作用,我无法在网上找到解决方案.
所以这是我的代码:
test.PHP的
<?PHP
include_once 'db_class.PHP';
$cat = $_GET['cat'];
$dbconn = new dbconn('localhost', 'root', 'somepsw', 'blog');
$dbconn->set_query("select * from posts where category = '".$cat."'");
echo '<br/>'.$dbconn->query.'<br/>';
$result = $dbconn->result;
$num = $dbconn->num_results;
$array = MysqLi_fetch_assoc($result);
echo json_encode($array);
?>
如果我在浏览器上输入该URL:http://127.0.0.1:82 / blog / sws / test.PHP?cat = css
通过jsonEncode返回的数据是正确的,但是当我在带有jquery的html页面上加载它时,他无法读取数据.
的test.html
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
function ajaxCall() {
var css;
$.ajax({
url: 'test.PHP',
type: "GET",
data: {cat: css},
dataType: 'json',
success: function(rows)
{
alert(rows);
},
error: function() { alert("An error occurred."); }
});
}
ajaxCall();
</script>
</head>
<body></body>
</html>
提前致谢.
解决方法:
你的变量css没有价值.你想使用字符串’css’.也许您希望能够加载其他类别.所以将ajaxCall函数更改为
function ajaxCall(category)
{
$.ajax({
url: 'test.PHP',
type: "GET",
data: {cat: category},
dataType: 'json',
success: function(rows) {
alert(rows);
},
error: function() {
alert("An error occurred.");
}
});
}
并使用它来调用它
ajaxCall('css');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。