2012-04-18 84 views
0

Im新的Web編程。我在這裏使用ajax時遇到問題,並且正在閱讀返回的json beig。 要求是使用Internet Explorer瀏覽器,所以我真的沒有選擇,只能使用它。 我的問題是功能沒有被執行成功。我不確定爲什麼,即使錯誤沒有顯示出來,我應該怎麼離開這裏。我的一位同事剛剛離開並辭職,現在我正在做他的工作。jQuery.ajax不能讀取成功:function()

下面是我的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)); 
     } 
    } 
} 

我真的很感激,如果你能幫助我傢伙...

+2

是否http://yourserver.com/WallboardHandler.ashx工作? – mplungjan 2012-04-18 09:31:26

+0

在你提出問題之前,請解決語法錯誤*(除非真的很難找到或者你不明白它)。如果你看了一下控制檯,或者使用像JSHint這樣的工具,你會看到它。 – 2012-04-18 09:48:34

回答

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 
       }); 
+0

Drat - 當我重新格式化成可讀的東西時,你會打敗我 – mplungjan 2012-04-18 09:38:43

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); 
    } 
    }); 
}); 
相關問題