2014-09-20 23 views
1

我希望有人告訴我下面的代碼有什麼問題,以及爲什麼它不工作。我不能得到一個列表<>使用Jquery.ajax

webmethod in aspx.cs頁面。

[webmethod] 
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public static List<Problem> GetProblems() 
    { 
     List<Problem> allproblems = new List<Problem>(); 
     using (TMEntities tm = new TMEntities()) 
     { 
      allproblems = tm.Problems.ToList();  
     } 
     return allproblems; 
    } 

以下是HTML

<script type="text/javascript"> 
    $(document).ready(function() { 
      $.ajax({ 
       type:"POST", 
       url: "WebForm1.aspx/GetProblems", 
       data: "{}", 
       datatype: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        alert('success'); 
       }, 
       failure: function (response) { 
        alert("fail"); 
       } 
      }); 
     }); 
</script> 

當我運行的應用程序沒有發生任何事情,當我按下Ctrl + Shift + J看到在瀏覽器中的錯誤,下面的錯誤出現

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 
+0

你如何期望重定向到ajax url中的頁面方法?正確的URL WebForm1.aspx?GetProblems = true之後,檢查page_load中是否爲響應參數GetProblems,如果爲true,則調用GetProblems方法。 – mybirthname 2014-09-20 09:32:17

+2

當你調試你的'GetProblems'方法時,你會看到什麼? (在開始處設置斷點,然後在調試器下運行Web應用程序。) – Richard 2014-09-20 09:33:17

+0

由於它在本地運行,在您的調試(客戶端)工具上,單擊網絡選項卡並查看500錯誤url,youw找到確切的錯誤,爲什麼服務器返回500.有可能是任何原因,一些Dll丟失或其他任何東西。因此,客戶端調試工具會給你原因。確保以反轉模式運行你的應用程序,反正你會這樣做。 – codebased 2014-09-20 09:59:25

回答

0

您的TMEntities構造函數是否初始化​​屬性? 如果不是,則在下面的行中爲空引用異常!

allproblems = tm.Problems.ToList(); 

如果不是上述問題,那麼您的Problem類有任何序列化問題?檢查一次

+0

我嘗試在頁面上運行相同的代碼加載並使GridView的數據源等於所有問題。 gridview1.datasource = allproblems; gridview1.databind(); 它工作正常,但不能與Ajax :(請幫助我 – Bassem 2014-09-20 13:08:35

0
[webmethod] 
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public static List<Problem> GetProblems() 
    { 
     List<Problem> allproblems = new List<Problem>(); 
     using (TMEntities tm = new TMEntities()) 
     { 
      allproblems = tm.Problems.ToList();  
     } 
     return allproblems; 
    } 

我檢查你的JavaScript代碼。這段代碼沒問題。 Bt問題是C#代碼。

請將[webmethod]更改爲[WebMethod]。我希望解決你的問題。如果不解決請給我你的ProblemTMEntities班級結構。

+0

好通知,我改變了它到WebMethod但仍然是同樣的問題:(,...........我嘗試在頁面加載運行相同的代碼,並使gridview的數據源等於allproblems。gridview1.datasource = allproblems; gridview1.databind ();它工作得很好,但不能與Ajax :(請幫助我 – Bassem 2014-09-20 17:26:13

+0

請使用jQuery數據表。請郵寄您的完整項目,我嘗試解決您的問題。 – 2014-09-21 04:14:58

+0

謝謝,但你的郵件是什麼? – Bassem 2014-09-21 17:04:37

相關問題