0

我是网络编程新手。我在这里使用 ajax 时遇到问题,我正在读取返回的 json beig。要求是使用 Internet Explorer 浏览器,所以我在这里真的别无选择,只能使用它。我的问题是该功能未成功执行。我不太确定为什么,因为即使是错误也没有显示任何内容,所以我应该如何从这里开始。这段代码是由我的一位刚刚离开并辞职的同事提供的,现在我最终接受了他的工作。

下面是我的 HTML 代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>ACES Wallboard</title>
        <link href="StyleSheet.css" rel="Stylesheet" type="text/css" />
        <script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
        <script type="text/javascript">
            <!--
            //var obj = jQuery.parseJSON('{"key": [{ "name": "ciq" },{ "name": "lwt" }],"value": [{ "ciq": "3", "lwt": "12" }]}');
            //window.onload = function() {
            //while (true) {
            //ajax request
            //for (var i = 0; i < obj.key.length; i++) {
            //    jQuery("#" + obj.key[i].name).html(obj.value[0][obj.key[i].name]);
            //}
            //sleep
            //}
            //}
            window.onload = function () {

                function errorAlert(e, jxr)
                {
                    alert("Your request is not successful. " + jxr);
                }

                jQuery.ajax(
                    {
                        type: "POST",
                        async: true,
                        url: 'WallboardHandler.ashx',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (obj) {
                            for (var i = 0; i < obj.key.length; i++) {
                                jQuery("#" + obj.key[i].name).html(obj.value[0][obj.key[i].name]);
                            }
                        }
                        error: errorAlert
                    });
            };
            //-->
        </script>
    </head>
    <body>
        <div>
            <div class="panelstyle">
                <div class="ciqrowstyle">Call in Queue: <span id="ciq">&nbsp;</span> Calls</div>
                <div class="lwtrowstyle">Longest Waiting Time: <span id="lwt">&nbsp;</span></div>
                <div>
                    <table style="border:5px solid black;font-family:Tahoma;font-size:50px;color:black" width="100%">
                        <tr style="background-color:SteelBlue;font-weight:bold;color:White;">
                            <td class="gridheader">&nbsp;</td>
                            <td class="gridheader">Avail</td>
                            <td class="gridheader">Unavail</td>
                            <td class="gridheader">Eng</td>
                            <td class="gridheader">Other</td>
                        </tr>
                        <tr>
                            <td class="rowstyle">HQ</td>
                            <td id="11" class="rowstyle">&nbsp;</td>
                            <td id="12" class="rowstyle">&nbsp;</td>
                            <td id="13" class="rowstyle">&nbsp;</td>
                            <td id="14" class="rowstyle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="alternatingrowstyle">CDA</td>
                            <td id="21" class="alternatingrowstyle">&nbsp;</td>
                            <td id="22" class="alternatingrowstyle">&nbsp;</td>
                            <td id="23" class="alternatingrowstyle">&nbsp;</td>
                            <td id="24" class="alternatingrowstyle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="rowstyle">Div 1</td>
                            <td id="31" class="rowstyle">&nbsp;</td>
                            <td id="32" class="rowstyle">&nbsp;</td>
                            <td id="33" class="rowstyle">&nbsp;</td>
                            <td id="34" class="rowstyle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="alternatingrowstyle">Div 2</td>
                            <td id="41" class="alternatingrowstyle">&nbsp;</td>
                            <td id="42" class="alternatingrowstyle">&nbsp;</td>
                            <td id="43" class="alternatingrowstyle">&nbsp;</td>
                            <td id="44" class="alternatingrowstyle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="rowstyle">Div 3</td>
                            <td id="51" class="rowstyle">&nbsp;</td>
                            <td id="52" class="rowstyle">&nbsp;</td>
                            <td id="53" class="rowstyle">&nbsp;</td>
                            <td id="54" class="rowstyle">&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="alternatingrowstyle">Div 4</td>
                            <td id="61" class="alternatingrowstyle">&nbsp;</td>
                            <td id="62" class="alternatingrowstyle">&nbsp;</td>
                            <td id="63" class="alternatingrowstyle">&nbsp;</td>
                            <td id="64" class="alternatingrowstyle">&nbsp;</td>
                        </tr>
                    </table>
                </div>
                <div class="dtrowstyle">Date: <span id="timestamp">&nbsp;</span></div>
            </div>
        </div>
    </body>
</html>

下面是我的 WallboardHander.cs 的代码,但我不知道为什么在 url 上被称为 ashx?

/*
 * Copyright (C) 2009 CanaPlus Consulting Pte Ltd
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Com.Canaplus.Commons.Config;
using Com.Canaplus.Commons.Logging;
using Newtonsoft.Json;

namespace Com.Canaplus
{
    public class WallboardHandler : IHttpHandler
    {
        private static ILogger _logger;

        protected string GetConnectionString()
        {
            return ConfigurationManager.GetInstance().GetConfiguration().GetConnectionString("SS1CMSDBC1");
        }

        protected static ILogger GetLogger()
        {
            return _logger ?? (_logger = LoggerFactory.GetLogger(typeof (WallboardHandler).FullName));
        }

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            var data = new Dictionary<string, ICollection<JsonType>>();
            var keys = new LinkedList<JsonType>();
            data.Add("key", keys);
            var values = new LinkedList<JsonType>();
            data.Add("value", values);
            var value = new ValueType();
            values.AddLast(value);
            try
            {
                using (var conn = new SqlConnection(GetConnectionString()))
                {
                    conn.Open();
                    var spGetTotalCallInQueue = new SqlCommand("sp_getToTalCallInQueue", conn)
                                                    {CommandType = CommandType.StoredProcedure};
                    var o = new KeyType {name = "ciq"};
                    keys.AddLast(o);
                    value.ciq = spGetTotalCallInQueue.ExecuteScalar().ToString();

                    var spGetWallBoardMatrixTb = new SqlCommand("sp_getWallBoardMatrixTB", conn)
                                                     {CommandType = CommandType.StoredProcedure};

                    var spGetMaxLongestWaitingTime = new SqlCommand("sp_getMaxLongestWaitingTime", conn)
                                                         {CommandType = CommandType.StoredProcedure};
                    o = new KeyType { name = "lwt" };
                    keys.AddLast(o);
                    value.lwt = spGetMaxLongestWaitingTime.ExecuteScalar().ToString();

                    var spGetCurrentDateTime = new SqlCommand("sp_getCurrentDateTime", conn)
                                                   {CommandType = CommandType.StoredProcedure};
                    o = new KeyType { name = "timestamp" };
                    keys.AddLast(o);
                    value.timestamp = spGetCurrentDateTime.ExecuteScalar().ToString();
                }
            }
            catch(Exception e)
            {
                GetLogger().Error(e);
            }
            context.Response.ContentType = "text/json";
            context.Response.Write(JsonConvert.SerializeObject(data, Formatting.Indented));
        }
    }
}

如果你能帮助我,我真的很感激......

4

2 回答 2

2

您的 AJAX 调用中有语法错误。成功函数后需要一个逗号。尝试用这个替换它:

            jQuery.ajax(
                {
                    type: "POST",
                    async: true,
                    url: 'WallboardHandler.ashx',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (obj) {
                        for (var i = 0; i < obj.key.length; i++) {
                            jQuery("#" + obj.key[i].name).html(obj.value[0][obj.key[i].name]);
                        }
                    },
                    error: errorAlert
                });
于 2012-04-18T09:32:19.933 回答
0

您在错误处理程序之前缺少逗号。

我会试试这个

<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>        
<script type="text/javascript">
$(document).ready(function () {
  $.ajax({
    type: "POST",
    async: true,
    url: 'WallboardHandler.ashx',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (obj) {
      for (var i = 0; i < obj.key.length; i++) {
        $("#" + obj.key[i].name).html(obj.value[0][obj.key[i].name]);
      }
    },
    error: function(e, jxr) {
      alert("Your request is not successful. " + jxr);
    }
  });
});
于 2012-04-18T09:35:16.807 回答