【问题标题】:I can not get a list<> using Jquery.ajax我无法使用 Jquery.ajax 获取列表<>
【发布时间】: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


【解决方案1】:

您的 TMEntities 构造函数是否正在初始化 Problems 属性? 如果不是,则下一行中的 null 引用异常!

allproblems = tm.Problems.ToList();

如果不是上述问题,那么您的Problem 课程是否有任何序列化问题?检查一次

【讨论】:

  • 我尝试在页面加载时运行相同的代码并使 gridview 的数据源等于 allproblems 。 gridview1.datasource=所有问题; gridview1.databind();它工作正常,但不适用于 ajax :(,请帮助我
【解决方案2】:
[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]。我希望能解决你的问题。如果不能解决,请给我你的Problem 和TMEntities 类结构。

【讨论】:

  • 请注意,我将其更改为 WebMethod 但仍然是同样的问题 :( ,......我尝试在页面加载时运行相同的代码并制作数据源一个 gridview 等于 allproblems . gridview1.datasource=allproblems; gridview1.databind(); 它工作正常,但不适用于 ajax :( ,请帮助我
  • 请使用 jQuery 数据表。请邮寄您的完整项目。我试图解决你的问题。
  • sbanerjee.cse@gmail.com
猜你喜欢
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 2016-12-05
  • 2015-10-05
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多