2011-10-05 42 views
1

我有jQuery函數:訪問來自asp.net返回對象jQuery中

function CheckStatus() { 
     $.ajax({ 
      url: "http://localhost:1111/test.myControl/GetDone", 
      type: "GET", 
      dataType: "jsonp", 
      success: function (data) { 
       console.info(data.GetStatusResult); 
       if (data.IsDone) { 
        result = true; 
        CheckForFile(); 
       } 
       else 
        result = false;  
      } 
     }); 

    } 

在這裏,我從asp.net MVC3調用該函數:

public MStatus GetDone() 
     { 
      ...... 

      return new MStatus { IsDone = IsDone, NowDateTime=DateTime.Now}; 
     } 

當你看到這個函數返回對象MStatus與變量IsDone。我試圖檢查IsDone的值,但我不能。

有人能告訴我如何解決我的問題?

+1

您需要澄清您的問題。你試圖達到什麼目的?你有什麼錯誤/困難?問題越清晰,您將收到回答的速度越快:) –

+0

使用Firebug查看ajax調用返回的結果 – Waqas

+0

You Right!但我的英語不太好,但是我發現我的錯誤,我必須更換行:data.IsDone與:data.GetStatusResult.IsDone – TwTw

回答

0

嘗試是這樣的: -

if(data.d.IsDone){ } 

希望這有助於

編輯: -

function GetTrainingDetails(id) { 
     $('#<%=updProgress.ClientID%>').css("display", "block"); 
     $.ajax({ 
      type: "POST", 
      url: "/_layouts/TrainingSheet/TrainingSourceBrowse.aspx/GetTrainingDetail", 
      data: "{'trainingId': '" + id + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { 
       AjaxSucceeded(msg); 
      }, 
      error: AjaxFailed 
     }); 
    } 

    function AjaxSucceeded(result) { 
     var res = result.d; 

     $('#<%= lblType.ClientID%>').html(res.TrainingType); 
     $('#<%= lblOrganizer.ClientID %>').html(res.Organizer); 
     $('#<%= lblTrainer.ClientID %>').html(res.Trainer); 
     $('#<%= lblLevel.ClientID %>').html(res.Level); 
     $('#<%= lblCategory.ClientID %>').html(res.Category); 
     $('#pDetails').html(res.Summary); 
     $('#headingTitle').html(res.Training); 

     $('#<%=updProgress.ClientID %>').css("display", "none"); 
     $('#divDetails').slideDown("slow"); 
    } 

    function AjaxFailed(result) { 
     alert(result.status + ' ' + result.statusText); 
     $('#<%=updProgress.ClientID %>').css("display", "none"); 
    } 
+0

爲什麼這樣工作? – jgauffin

+0

它的作品!謝謝! – TwTw

0

檢查請求是AJAX請求和發送JSON包

var status = new MStatus { IsDone = IsDone, NowDateTime=DateTime.Now}; 
if (Request.IsAjaxRequest()) { 
    return Json(new { IsDone = status.IsDone, NowDateTime = status.NowDateTime } 
       , JsonRequestBehavior.AllowGet); 
}else{ 
    return status; 
} 
0

Your method nee d返回ActionResult

public ActionResult GetDone() 
{ 
    ...... 

    var status = new MStatus { IsDone = IsDone, NowDateTime=DateTime.Now}; 
    return Json(status, JsonRequestBehavior.AllowGet); 
}