我对这个动态查询做错了什么

What am i doing wrong with this dynamic query?

本文关键字:错了 什么 查询 动态      更新时间:2024-05-13

我以前从未使用过AJAX或JQuery,但以下是我对动态加载的尝试(来自stackoverflow的各种示例)

这是我认为的脚本:(经过编辑以符合mayabelle的代码。)不会抛出任何警报,DRequest上的断点也不会跳闸,但如果直接调用DRequest,则会产生结果。

<script type="text/javascript">
$(document).ready(function () {
alert("testing123");
$response = DRequest;
alert("good at response");
$.ajax({
        url: "request/drequest"
        type: "GET",
        dataType: "json",
        success: function ($response) {
                    alert("I am an alert box2!");
            // Do something with your response
            var $tr = $('<tr>').append(
        $('<td>').text($response.NeededByDate),
        $('<td>').text($response.RequestedBy),
        $('<td>').text($response.Username),
        $('<td>').text($response.RequestedPCID),
        $('<td>').text($response.RequestType_ID),
        $('<td>').text($response.Division_ID),
        $('<td>').text($response.ReqTypeIcon)
    ).appendTo('#requestTable');
            console.log($tr.wrap('<p>').html());
        }   
    });
    setInterval(function () {
        var url = '#';
        $('body').load(url);
    }, 300000);
});  
</script>

应该从DRequest JsonResult一次动态地追加一行(直到没有更多的行可添加为止)(当通过地址栏直接调用时,这会产生结果)。这应该每5分钟(300000秒)重新加载整个页面。

JsonResult看起来像这个

    Public Function DRequest() As JsonResult
        Dim Reqs = _db.dRequestGetAll
        Return Json(Reqs, JsonRequestBehavior.AllowGet)
    End Function

其中"_db.dRequestGetAll"返回dRequest行的集合,如下所示:

Public Function dRequestGetAll() As IEnumerable(Of DRequest)
    Return From r In _PITcontext.Requests Where r.CompletedDate Is Nothing Select r
End Function

所以。我错过了什么?

编辑:我用最新版本替换了原始帖子中的javascript,因为评论不能处理超过600个字符。

这样尝试:

$(document).ready(function () {
    $.ajax({
            url: url to your controller action,
            type: "GET",
            dataType: "json",
            success: function (response) {
                // Do something with your response
            }   
        });
}

此外,在上面的代码中,您正在调用变量$response,但在每个循环中,您都试图访问response(没有$前缀)。

我认为应该使用$.map()而不是$.each()。它返回元素的数组。此处讨论差异。