Calling a function from Ajax on Asp.Net/Razor in C#

Calling a function from Ajax on Asp.Net/Razor in C#

haigdahaigda Posts: 5Questions: 1Answers: 0

Description of problem:
I have used DataTables in the past with MVP pages but am working with Razor and # this time. I have setup my DataTable on the page RTSkills.cshtml and configured the Ajax call as below -

On the RTSkills.cshtml.cs page I have my function -

Which I have tested and is returning JSON correctly when tested as a standalone function.

The problem is it doesn't seem to be returning it when 'requested' by DataTables. I don't think the function is actually being called as I have a breakpoint set on entry which is not 'triggered'. however I am getting a "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" when I look in the console window of the browser (FireFox)

Have you any tips on how to try and debug this? Or any ideas as to what I am doing wrong?

Cheers,
David

Answers

  • kthorngrenkthorngren Posts: 21,082Questions: 26Answers: 4,908

    I'm not familiar with your server environment so can't help there. I would first use the browser's network inspector tool to see what the response is. Possibly its an error message that can point you in the right direction.

    Kevin

  • haigdahaigda Posts: 5Questions: 1Answers: 0

    Thanks Kevin,
    That has helped a bit - I can see that the OnGet() call is being accessed from the RTSkills page but it is not calling the OnGetLoadDataSkillsetAsync()

    So it seems I am returning the view/page itself.

    I did have the default OnGet() commented out and there was no difference. I think my problem is that I'm not calling the OnGetLoadDataSkillsetAsync() but I can't see what I'm doing wrong in the code below - any ideas anyone?

    Cheers,
    David

  • allanallan Posts: 62,990Questions: 1Answers: 10,367 Site admin

    Hi David,

    Nothing obvious on the client side there. I can see from the screenshot that the browser is making a request to /RTSkills?handler=LoadDataSkillsetAsync. If that should be calling OnGetLoadDataSkillsetAsync but isn't, I'm afraid that there is likely a routing problem somewhere in the app, and that's something you might need to ask on SO or some .NET specific forum about.

    Allan

  • haigdahaigda Posts: 5Questions: 1Answers: 0

    Thanks Allan,
    I will post to SO and see what they say.
    Cheers,
    David

  • allanallan Posts: 62,990Questions: 1Answers: 10,367 Site admin

    I'd be interested to know what it is, if you get it figured out and have a chance to post back here. .NET routing is something I need to learn a bit more about myself!

    Allan

  • haigdahaigda Posts: 5Questions: 1Answers: 0

    Hi Allan,
    I have solved my issue - it seems if you set the function being called as a Task and 'async' the Ajax call won't call it - I suspect this is due to not being able to deal with the returned 'Task'.
    So I have taken out all the async and changed the function from a task to a 'normal' function and moved on to the next error!
    In respect of that, is there any way of displaying the raw data that is returned to the DataTable? I can see the list has been created OK but DataTables is giving me a Requested Unknown Parameter error.
    Cheers,
    David

  • kthorngrenkthorngren Posts: 21,082Questions: 26Answers: 4,908

    is there any way of displaying the raw data that is returned to the DataTable?

    Use the browser's network inspector tool to see the JSON response. This technote provides instructions to do this.

    DataTables is giving me a Requested Unknown Parameter error

    Use this technote for troubleshooting steps.

    Let us know what you find.

    Kevin

  • haigdahaigda Posts: 5Questions: 1Answers: 0

    Thanks Kevin,
    All sorted now - I capitalised the column name in the "Columns" but the json keys are lowercase.
    Cheers,
    David

  • kthorngrenkthorngren Posts: 21,082Questions: 26Answers: 4,908

    Good find. That will cause problems :smile:

    Kevin

Sign In or Register to comment.