0

背景

我有一个 Web 应用程序,它显示有关帐户的“简单”信息,即姓名和帐号……我在侧面有一个按钮来显示“详细”信息……即姓名、帐户号和电话号码。

系统需要在单击此按钮时动态提取信息,并将填充到指定的 div 中......在这种情况下我做错了什么,因为我不断收到“错误加载信息”的警报编辑: -将错误警报更改为警报(textStatus)-现在我所拥有的只是警报框中的“错误”......

JS/阿贾克斯

$('.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);
                    }
                    });

JSON

<?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); 
 ?>
4

1 回答 1

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!");}
于 2013-04-28T18:47:27.030 回答