javascript - 第二次回调 ajax web 方法的问题

标签 javascript jquery asp.net ajax

所以我有这个ajax方法可以帮助我从表中查找选定的行,并将信息显示到一系列字段中。问题是第一次调用电话时效果非常好。但是,一旦我搜索另一条记录,数据就不会显示,并且我在网络资源管理器控制台中收到此错误:

Uncaught TypeError: Cannot read property 'Detalles' of null

'详细信息?是 json 带我回来的数组。所以我认为请求的时间有问题,对此有什么建议吗?这是ajax方法:

return $.ajax({
        type: "POST",
        url: "../../../ServiciosWeb.asmx/CargarDetallesRequisicion",
        data: '{IdRequisicion:"'+ idRequisicion +'" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false
});

我希望有人能帮助我...谢谢!

更新: 这是将我的信息转换为 JSon 的方法

Function DataSetToJSON(ds As DataSet) As String
        Dim dict As New Dictionary(Of String, Object)

        For Each dt As DataTable In ds.Tables
            Dim arr(dt.Rows.Count - 1) As Object

            For i As Integer = 0 To dt.Rows.Count - 1
                arr(i) = dt.Rows(i).ItemArray
            Next

            dict.Add(dt.TableName, arr)
        Next

        Dim json As New JavaScriptSerializer
        Return json.Serialize(dict)
    End Function

当指定为空时,我检查了数据的返回,是的,它带来了信息,这就是为什么对我来说与时间有关。这是我调用 $.ajax 方法的地方:

 if (idRequisicion > 0) {
        console.log(idRequisicion);
        $.when(
        CargarDetalles()
        ).done(MostrarDetalles);
    }

这个条件放在$(document).ready

已解决:

事情是这样的:

if (idRequisicion > 0) {

        function MostrarInformacion(fn, tiempo) {

            var dfd = $.Deferred();

            setTimeout(function () {

                dfd.resolve(fn());
            }, tiempo || 0);

            return dfd.promise();
        }
        var promise = MostrarInformacion(function () {
            $.blockUI({
                css: {
                    border: 'none',
                    padding: '0',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff',
                    onBlock: $.when(CargarDetalles()).done(MostrarDetalles)
                }
            });
            setTimeout($.unblockUI, 2000);

            }, 300);

    }

忽略 BlocUI,这是新的。总之,这解决我的问题可能不是最有效的方法,但目前效果很好。

最佳答案

jQuery.when 需要延迟对象:

jQuery.when( deferreds ) Returns: Promise

Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

您应该将 ajax 请求直接放在 $.when 中,而不是函数调用中,如下所示:

 $.when(
       $.ajax({
        type: "POST",
        url: "../../../ServiciosWeb.asmx/CargarDetallesRequisicion",
        data: '{IdRequisicion:"'+ idRequisicion +'" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false
}
        ).done(MostrarDetalles);

如果您需要相同的设置,也许您应该只返回 ajax 设置,例如 $.ajax(CargarDetalles()) 而不是完整的请求

关于javascript - 第二次回调 ajax web 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772431/

相关文章:

javascript - jquery - .hash 在此函数中的意义

javascript - jQuery - 输入文本属性未显示在页面上

javascript - jQuery无限双向动画

javascript - 查询 : How to disable mouse move vertically?

c# - asp.net - 绑定(bind)到 linq 数据源并添加外部列

c# - 如何在弹出另一个窗口请求信息后将文本插入文本框?

c# - 找不到类型或命名空间(但应该是!)

javascript - 内容更新后 jScrollPane 不滚动到底部

javascript - 处理 AJAX 站点的哈希 URL 的更好方法

JavaScript 和谐 - 它是什么?