我想要做的是通过ajax和php调用一些数据库数据。但ajax调用不起作用,我无法在网络上找到解决方案。通过ajax调用加载mysqli的php数据
因此,这里是我的代码:
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:通过jsonEncode http://127.0.0.1:82/blog/ws/test.php?cat=css
返回的数据是正确的,但是当我用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>
感谢。
AJAX调用使用 '/test.php' 调用它。浏览器正在使用'/blog/ws/test.php'。除非你正在重写你没有显示的东西。 – markdwhite
你不会从你的test.php中得到纯粹的json数据,因为你已经打印了一些echo'
'。$ dbconn-> query。'
';从你的代码中除去只有json_encode的所有回声。 –
@ManishJangir建议做什么,然后将'var css;'更改为'var css =“css”;'它会起作用。 – Joe