【发布时间】:2014-11-14 20:07:33
【问题描述】:
我希望有人告诉我下面的代码有什么问题,以及为什么它不起作用。
aspx.cs 页面中的 webmethod。
[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 中检查 Response 参数 GetProblems 是否为 true 调用方法 GetProblems。
-
调试
GetProblems方法时看到了什么? (在开始时设置断点,然后在调试器下运行 Web 应用。) -
由于它是在本地运行,在您的调试(客户端)工具上,单击网络选项卡并查看 500 错误 url,您将找到服务器返回 500 的确切错误。可能是任何原因,一些DLL丢失或其他任何东西。因此,客户端调试工具会给你原因。确保以 DEBUG 模式运行您的应用程序,无论如何您都会这样做。
-
@mybirthname 请澄清更多
-
@Richard 什么都没发生,步入和跨过都变暗/禁用
标签: c# jquery asp.net ajax web-services