这是我的ajax电话
<script src = "js/jquery.js" type = "text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#DepartmentNameDropdown').change(function(){
$('#DepartmentName').attr('value', document.getElementById("DepartmentNameDropdown").options[document.getElementById("DepartmentNameDropdown").selectedIndex].text);
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost:8081/crownregency/getInfo.php",
data: {id : $('#DepartmentNameDropdown').val()}, //data: {status: status, name: name},
success:function(data){
$('#DepartmentDescription').attr('value', data.Desc);
}
});
});
});
$('#DepartmentName').attr('value', $('#DepartmentNameDropdown').text());
</script>
这是我的getinfo.php
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("jkings");
$value = $_POST['id'];
$query = mysql_query("SELECT * FROM department WHERE dept_ID = $value");
$row = mysql_fetch_array($query);
$desc = $row['dept_desc'];
$mission = $row['dept_mission'];
$struct = array('Desc' => '$desc', 'Mission' => '$mission');
session_start();
$_SESSION['id'] = $value;
echo json_encode($struct);
//echo $row['dept_desc'];
?>
我不知道代码在哪里犯了一个错误,但似乎我无法访问json响应。我尝试了不同的方法,我还在学习这个ajax和json,所以我很难看到我犯了错误
答案 0 :(得分:0)
我不完全确定,但我认为你可能需要改变
$('#DepartmentDescription').attr('value', data.Desc);
到
$('#DepartmentDescription').attr('value', data['Desc']);
此外,
$ struct = array('Desc'=&gt;'$ desc','Mission'=&gt;'$ mission');
应改为
$ struct = array('Desc'=&gt; $ desc,'Mission'=&gt; $ mission);
答案 1 :(得分:0)
你的javascript似乎很好我会建议这可以检测问题可能在哪里
$.ajax({
type: "POST",
dataType: "json",
url: "http://localhost:8081/crownregency/getInfo.php",
data: {id : $('#DepartmentNameDropdown').val()}, //data: {status: status, name: name},
success:function(data){
alert(data.Desc)// should alert [object object] if all is well with your php code
$('#DepartmentDescription').attr('value', data.Desc);
}
});
我真的很怀疑,但可能是一个区分大小写的问题,所以可能会改变
dataType:'json' to dataType:'JSON'
如果你正在使用chrome use developers工具来查看是否存在javascript错误(为safari开发)