【问题标题】:Simple ajax call getting exception简单的ajax调用获取异常
【发布时间】:2014-03-06 16:06:49
【问题描述】:

我有这个 ajax 调用:

<script src="js/jquery-1.11.0.js"></script>
  <script type="text/javascript">
        $(document).ready(function () {
            // Add the page method call as an onclick handler for the div.
            $("#Result").click(function () {

                $.ajax({
                    type: "POST",
                    url: "Default.aspx/GetDate",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        // Replace the div's content with the page method's return.
                        $("#Result").html(msg.d);
                    }
                });
            });
        });
</script>

HTML:

<div id="Result">Click here for the time.</div>

网络方法:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
    Public Shared Function GetDate() As String
        Return DateTime.Now.ToString()
    End Function

当我运行它时,我在控制台中遇到错误:

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

谁能说出我哪里出错了?如果我下载提供的示例并且它工作正常,这在 Encosia 的 example 中提到。

【问题讨论】:

  • 能否请您尝试返回 DateTime.Now.toString() 以外的静态字符串并尝试查看它是否有效?因为错误 500 表明您的服务器端存在问题.. AJAX 和 Havascript 都没有
  • 实际上按钮本身没有触发...
  • 500 错误表示服务器端功能正在抛出异常,您需要查找该异常是什么。来自服务器的响应正文是否包含错误消息?如果您手动加载它而不是通过 AJAX 调用,响应是什么?如果将调试器附加到服务器端代码,是否会引发异常?
  • 如果在 GetDate() 函数上设置断点,它会被命中吗?
  • @David-我怎样才能找出那个异常?响应没有错误。

标签: jquery asp.net ajax vb.net


【解决方案1】:

不是专家,但我在一些作品中遇到过这种情况。 这是供您测试的:

$(document).on("click", "#Result", function(){
    var data = {};
    $.post("Default.aspx/GetDate", data, function(msg,status){
        $(this).html(msg.d);
        console.log(msg);
    })
})

【讨论】:

  • 现在没有收到 500 内部服务器错误但没有触发结果 div 的点击..
  • 我已经更新了绑定点击事件的答案。也许你有一个动态加载的页面?控制台说什么?
  • 顺便说一句,这实际上很容易出错,因为它会在每次页面加载时绑定点击,并且最终会进行多次绑定。识别您的容器并替换 $(document)
【解决方案2】:

不知道发生了什么,但这通过添加解决了我的问题:

<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 2019-02-15
    • 2012-08-29
    • 2011-07-18
    相关资源
    最近更新 更多