简体   繁体   中英

What am i doing wrong with this dynamic query?

i've never used AJAX or JQuery before, but here's my attempt at dynamic loading(pulled from various examples here at stackoverflow)

this is the script i have in my view:(edited to comply with mayabelle's code.) doesn't throw either alert, and the breakpoint on DRequest never trips, but drequest produces results if called directly.

<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>

is supposed to dynamically append one row at a time (until there are no more rows to add) from the DRequest JsonResult (this is producing results when called directly by way of the addressbar). this should reload the whole page every 5 minutes(300000 seconds).

the JsonResult looks like this

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

where "_db.dRequestGetAll" returns a collection of dRequest rows like so:

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

so. what did i miss?

EDIT: i replaced the javascript from the original post with the most current version since comments can't handle more than 600 characters.

I think you should be using $.map() instead of $.each(). It returns an array of your elements. Differences are discussed here.

Try like this:

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

Also, in your code above you are calling your variable $response but then in your each loop you are trying to access response (no $ prefix).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM