我希望有人告訴我下面的代碼有什麼問題,以及爲什麼它不工作。我不能得到一個列表<>使用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)
你如何期望重定向到ajax url中的頁面方法?正確的URL WebForm1.aspx?GetProblems = true之後,檢查page_load中是否爲響應參數GetProblems,如果爲true,則調用GetProblems方法。 – mybirthname 2014-09-20 09:32:17
當你調試你的'GetProblems'方法時,你會看到什麼? (在開始處設置斷點,然後在調試器下運行Web應用程序。) – Richard 2014-09-20 09:33:17
由於它在本地運行,在您的調試(客戶端)工具上,單擊網絡選項卡並查看500錯誤url,youw找到確切的錯誤,爲什麼服務器返回500.有可能是任何原因,一些Dll丟失或其他任何東西。因此,客戶端調試工具會給你原因。確保以反轉模式運行你的應用程序,反正你會這樣做。 – codebased 2014-09-20 09:59:25