AJAx脚本要么没有连接到testData.php,要么无法从testData.php中提取数据。无法隔离问题,任何帮助将不胜感激。
我有2个文件
index.php - AJAX脚本所在的位置以及我想要的数据显示在getdiv标记中。
textData.php - 使用mysql db来提取数据。这个脚本有效,我在底部复制/粘贴结果。
我认为问题出在AJAX脚本的某个地方。如果不能在div标签中显示信息,它不会提取信息。
testData.php - 数据库脚本和结果:
<?php
require_once('dbconv2.php');
$dbcon1 = Connection::getConnection();
try { $stmt = $dbcon1 ->query("SELECT
click_count.page_ur,
click_count.page_count
FROM click_count
");
$testArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $row = $stmt->rowCount();
}
catch(PDOException $e)
{ echo $e->getMessage(); }
var_dump($testArray);
echo json_encode($testArray);
?>
array (size=7)
0 =>
array (size=2)
'page_ur' => string 'test record page1' (length=17)
'page_count' => string '1' (length=1)
1 =>
array (size=2)
'page_ur' => string 'test record page2' (length=17)
'page_count' => string '4' (length=1)
2 =>
array (size=2)
'page_ur' => string 'test record page3' (length=17)
'page_count' => string '3' (length=1)
[{"page_ur":"test record page1","page_count":"1"},{"page_ur":"test record page2","page_count":"4"},{"page_ur":"test record page3","page_count":"3"},{"page_ur":"test record page5","page_count":"7"},{"page_ur":"test record page4","page_count":"9"},{"page_ur":"test record page6","page_count":"6"},{"page_ur":"test record page7","page_count":"2"}]
index.php - AJAX脚本
<body>
<div id="getdiv"> </div>
<div id="questionButton">
<form method="POST" name="form_questions" action="">
<BUTTON class="btn_get" id ="btn_get" NAME="btn_get">getInfo</BUTTON>
</form>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#btn_get').on('click', function(e) {
$.ajax({
type: 'GET',
cache: false,
url: 'testData.php',
dataType: 'json',
success:function(info){
$("#getdiv").html(info[0].page_ur);
}
});
e.preventDefault();
});
});
</script>
</body>