0

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.

2 Answers 2

0

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

3
  • I've modified the code above to reflect your suggestion. still doesn't ever throw the breakpoint on DRequest() table's still empty. Dec 10, 2013 at 1:33
  • I would suggest putting a breakpoint in your controller action to make sure it gets in there. If it does not, your url might be incorrect or routing may be set up differently.
    – mayabelle
    Dec 10, 2013 at 15:39
  • i went ahead and checked this as the answer because it's actually hitting the JSONresult. (i still get wonky results, but it's unrealted to the original issue.) thanks again! Dec 10, 2013 at 17:27
0

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

3
  • so the "$response = $.parseJSON(DRequest);" line looks OK? why isn't the alert getting thrown if the script isn't getting kicked out right there? I made the recommended change, but it still gives me an empty page. (well, i get a table header, but no data in it) Dec 9, 2013 at 18:47
  • Can you post the response that you would see in Firebug or Chrome ?
    – mitch
    Dec 9, 2013 at 18:55
  • If you are returning JSONResult, you don't need to parse it. The content type is set to json when using JsonResult.
    – mitch
    Dec 9, 2013 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.