ajax代码无法从php访问json响应
这是我的ajax电话ajax代码无法从php访问json响应,php,json,jquery,Php,Json,Jquery,这是我的ajax电话 <script src = "js/jquery.js" type = "text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $('#DepartmentNameDropdown').change(function(){ $('#DepartmentName').attr('value
<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,所以我很难看出我在哪里犯了错误,我不完全确定,但我想你可能必须改变 进入 而且 $struct=array'Desc'=>'$Desc','Mission'=>'$Mission' 应改为 $struct=数组'Desc'=>$Desc',任务'=>$Mission
您的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使用开发者工具查看是否存在javascript错误,请为safari开发首先更正此行$struct=array'Desc'=>$Desc',Mission'=>$Mission;
$.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'