我有一个Web应用程序,显示有关帐户的“简单”信息,即姓名和帐号....我在旁边有一个按钮,显示“详细”信息...即姓名,帐号#和电话#。
系统需要在点击此按钮时动态提取信息,并填充到指定的div中...在这种情况下我做错了什么,因为我经常收到“错误加载信息”的警报的。 编辑: - 更改警报的错误警告(textStatus) - 现在我所有的都是警告框中的“错误”....
$('.view_information').click(function(e) {
//On Clicking the function, dynamically load the data for the viewing
var dataObj = {};
dataObj["id"] = $(this).data('id');
dataObj["action"] = "get"; // "get" or "set"
$.ajax({
url: 'view_agency_info.php',
type: 'POST',
data: dataObj,
dataType: 'json',
success: function(data){
/* Doesn't Work....*/
$('.view_id').html(data.id);
$('.view_name').html(data.name);
$('.view_account').html(data.account);
$('.view_phone').html(data.phone);
/*Also Doesn't work...
$('.view_id').html(data.message['id']);
$('.view_name').html(data.message['name']);
$('.view_account').html(data.message['account']);
$('.view_phone').html(data.message['phone']);*/
},
error: function(jqXHR, textStatus, errorThrown){
// alert('Error Loading Information');
alert(textStatus);
}
});
<?php
include_once('customer.class.php');
$customer = new Customer();
$query = "SELECT * FROM table WHERE id=$id LIMIT 1"; //expecting one row
try {
$result = $customer->runQuery($query); //class function with fetchAll
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
$message = array(
'id' => $id,
'name' => $result[0]['agency_name'],
'account' => $result[0]['account_number'],
'phone' => $result[0]['phone']
);
}
print json_encode($message);
?>
答案 0 :(得分:0)
看起来我能够修复语法以使其可操作....如果将数组传递给json来执行。
我将JS / AJAX更改为:
/* Old Variable Definitions
//var dataObj = {};
//dataObj["id"] = $(this).data('id');
//dataObject["column"] = $(this).data('column');
//dataObj["action"] = "get"; // "get" or "set"*/
/* New Syntax: */
var data_id = $(this).data('id');
var data_action = "get";
var column_toact_on = $(this).data('column');
$.ajax({
url: 'view_agency_info.php',
type: 'POST',
//data: dataObj, // OLD array
data: {id : data_id, action: data_action, column: column_toact_on},
dataType: 'json',
success: function(data){
alert("Wooptee Doo Basil!");}