在下面的代码中有一个带有目标 url 的 XMLHTTPRequest ,test_location_jquery.php
尽管触发了成功函数,但永远不会访问该文件。当单独运行以进行测试时,test_location_jquery.php
已成功按预期返回 JSON 对象。任何关于如何在不访问文件时触发成功功能的想法将不胜感激。
查询
<?php
require_once '../meta/php/dbConn.php';
$charlie_query = "";
$charlie_result = "";
$charlie_query = "
SELECT ud.user_id as 'id',
concat(ud.last_name,', ',ud.first_name) as 'name'
FROM `user_detail` AS ud,
`user_type` AS UT
WHERE ud.user_type = ut.id
AND ut.class IN ('charlie')
AND ut.level IN (4)
ORDER BY ut.class;
";
$charlie_result = mysql_query($charlie_query);
if(!(bool)$charlie_result){
throw new Exception();
}
?>
<html>
<head>
<script src="../meta/js/jquery-1.7.2.js"></script>
<!-- <script src="../meta/js/getXML_sir.js"></script> -->
<script>
$(document).ready(function(){
$('#charlie').change(function(){
var charlie_id = $('#charlie option:selected').val();
var DT = "json";
$.post(
'test_location_jquery.php',
{'id':charlie_id,'datatype':DT},
function(data){
alert('Success');
var obj = $.parseJSON(data);
}
);
if(obj != 'undefined'){
var locations = obj.find('location');
console.log(locations);
}else{
alert("failure");
}
})
})
</script>
</head>
<body>
<form>
<label>Charlie: </label><select id="charlie" name="charlie">
<option> </option>
<?php
while($row = mysql_fetch_assoc($charlie_result)){
print("<option value='".$row['id']."'>".$row['name']."</option>");
}
?>
</select>
<label>Location: </label><select id="location" name="location"></select>
</form>
<div></div>
</body>
</html>
test_location_jquery.php
<?php
require_once("../meta/php/dbConn.php");
if(!isset($_POST)){
throw new Exception();
}else{
$id = $_POST['id'];
$datatype = $_POST['datatype'];
}
$query = "
SELECT l.id as 'id',
ld.attribute_string as 'name'
FROM `location` AS l,
`location_details` AS ld
WHERE l.id = ld.location_id
AND ld.attribute_label = 'name'
AND l.id = ".$id.";
";
$result = mysql_query($query);
if(!(bool)$result){
throw new Exception();
}else{
switch($datatype){
case 'xml':
break;
case 'json':
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
return json_encode($rows);
break;
}
?>