2010-10-03 161 views
1

大家好!問題與AJAX,JavaScript,ASP.Net MVC

  • 我在寫Asp.Net MVC 2網站。
  • 我有TimeController和TimeView,CountDownHelper在TimeView頁面上呈現時間。
  • 此外,我有更新當前時間的JavaScript,用於CountDownHelper。

我需要從此JavaScript調用AJAX以獲取服務器上的當前時間。 我該怎麼做?請幫幫我!我必須完成大約幾個小時!

下面你可能會看到這個javaScript,並在它的最後我嘗試調用AJAX。我試圖用幾種方式編寫GetServerTime.html,但是任何一個都不行。 (((

//countDown.js 
function calcage(secs, num1, num2) 
{ 
    s = ((Math.floor(secs/num1)) % num2).toString(); 
    if (LeadingZero && s.length < 2) 
     s = "0" + s; 
    return "<b>" + s + "</b>"; 
} 

function CountBack(secs) 
{ 
    if (secs < 0) 
    { 
     location.reload(true); 
     document.getElementById("cntdwn").innerHTML = FinishMessage; 
     return; 
    } 

    //difference between recieve time and current client time 
    diff = new Date(new Date() - clientTime); 
    targetD = new Date(TargetDate); 
    serverD = new Date(serverDate); 
    currentServerDate = new Date(serverD.getTime() + diff.getTime()); 

//targetD 
    leftD = new Date(targetD.getTime() - currentServerDate.getTime()); 

    secs = leftD.getTime()/1000; 

    DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs, 86400, 100000)); 
    DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs, 3600, 24)); 
    DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs, 60, 60)); 
    DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs, 1, 60)); 

    document.getElementById("cntdwn").innerHTML = DisplayStr; 
    if (CountActive) 
     setTimeout("CountBack(" + (secs + CountStepper) + ")", SetTimeOutPeriod); 
} 

function putspan(backcolor, forecolor) 
{ 
    document.write("<span id='cntdwn' style='background-color:" + backcolor + 
       "; color:" + forecolor + "'></span>"); 
} 

if (typeof (BackColor) == "undefined") 
    BackColor = "white"; 
if (typeof (ForeColor) == "undefined") 
    ForeColor = "black"; 
if (typeof (TargetDate) == "undefined") 
    TargetDate = "12/31/2020 5:00 AM"; 
if (typeof (serverDate) == "undefined") 
    serverDate = "12/31/2020 5:00 AM"; 
if (typeof (DisplayFormat) == "undefined") 
    DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds."; 
if (typeof (CountActive) == "undefined") 
    CountActive = true; 
if (typeof (FinishMessage) == "undefined") 
    FinishMessage = ""; 
if (typeof (CountStepper) != "number") 
    CountStepper = -1; 
if (typeof (LeadingZero) == "undefined") 
    LeadingZero = true; 


CountStepper = Math.ceil(CountStepper); 
if (CountStepper == 0) 
    CountActive = false; 
var SetTimeOutPeriod = (Math.abs(CountStepper) - 1) * 1000 + 990; 
putspan(BackColor, ForeColor); 
var dthen = new Date(TargetDate); 
var dtServ = new Date(serverDate); 
var dnow = new Date(); 
if (CountStepper > 0) 
    ddiff = new Date(dnow - dthen); 
else 
    ddiff = new Date(dthen - dtServ); 
    //ddiff = new Date(TargetDate - serverDate); 
//ddiff = new Date(dthen - dnow); 
gsecs = Math.floor(ddiff.valueOf()/1000); 
CountBack(gsecs); 

alert("Start"); 
alert(serverDate); 

//AJAX CALL ???? 
//How to call async JavaScript? 
//Which must be GetServerTime.html 

$.get('Views/GetServerTime.html', function(data) { 
    serverDate = data; 
    clientTime = new Date();  
}); 

alert(serverDate);** 
+0

你是什麼意思「異步JavaScript」? – kervin 2010-10-03 21:33:13

+0

異步調用JavaScript,即不刷新所有頁面 – Roman 2010-10-03 21:50:42

回答

2

通常你不能直接訪問你的觀點。而觀點通常是.ASPX文件。

所以

$.get('Views/GetServerTime.html',... 

變爲

$.get('/GetServerTime/',... 

對於Views/GetServerTime/Index.aspx視圖和getserverTimeController.cs控制器的默認操作爲索引

,但我想這不是你唯一的問題?...

編輯

你也應該使用JSON這一點。您可以使用System.Web.Mvc.JsonResult自動發送結果爲JSON,jQuery將處理此JSON並將其轉換爲javascript對象。

 $.get('/GetServerTime/', 
         function (data) 
         { 
           if (data.HasError == false) 
           { 
            $("#resultDiv").html(data.ServerTime); 
           } 
         }, "json"); 

你的MVC Action可以這個樣子......

public JsonResult Index(string id) 
{ 
    JsonResult res = new JsonResult();   
    res.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 

    res.Data = new { ServerTime = DateTime.Now(), HasError = false }; 

    return res; 
} 

以上是近似的,因爲我沒有一個編譯器。

+0

是的,哪一個頁面必須是GetServerTime? – Roman 2010-10-03 21:46:33

+0

嘿羅馬人,視圖文件夾通常映射到*控制器*和視圖名稱映射到*動作*。默認操作通常是* Index *,因此Index.aspx。這一切都可以很容易地改變。 – kervin 2010-10-03 21:49:19

+0

$ .get('Views/GetServerTime.html',function(data){ serverDate = data; clientTime = new Date(); }); 此編程是正確的? – Roman 2010-10-03 21:53:26

0

首先,我不確定「GetServerTime.html」會成功爲您提供當前時間。你確定它是你想要的頁面的名字嗎?除非您將路由設置爲處理該URL模式。正如Kervin下面所說。

此外,「函數(數據)」方法的主體是ajax函數返回時調用的回調函數。

至於你的頁面返回服務器的日期/時間,你需要決定它將返回的格式:XML或JSon。然後你的控制器會返回。

public class DateController : Controller { 
    public ActionResult CurrentDate() 
    { 
     var returnJson = new 
      { 
       currentDate = DateTime.Now.ToString() 
      } 

     return Json(returnJson, JsonRequestBehavior.AllowGet); 
    } 
} 

然後你的。get函數看起來像:

$.get('/Date' function(data) { 
    theDate = data.currentDate; 
});