3

My page is domain.com/home/details/1

In my jQuery AJAX call I have the following however when it makes that call its making a call to domain.com/home/details/home/getdata

What can I do to get it to resolve properly?

$(document).ready(function () {

            oTable = $('#example').dataTable({
                "bServerSide": true,
                "sAjaxSource": "Home/GetData/",
                "bProcessing": true,
                "bPaginate": true,
                "sPaginationType": "full_numbers",
                "bFilter": true,
                "bAutoWidth": false,
                "fnServerData": function (sSource, aoData, fnCallback) {
                    /* Add some extra data to the sender */
                    //aoData.push({ "filtervalue": $('#filtervalue').val(), "Options": $('#Options').val() });
                    $.getJSON(sSource, aoData.concat($('form').serializeArray()), function (json) {
                        /* Do whatever additional processing you want on the callback, then tell DataTables */
                        fnCallback(json)
                    });
                }
            });

        });

3 Answers 3

16

Absolutely always use URL helpers when dealing with urls in ASP.NET MVC. Absolutely never hardcode urls as you did.

So:

"sAjaxSource": "@Url.Action("GetData", "Home")"

and if this is in a separate javascript file, you could use HTML5 data-* attributes on the #example:

<div id="example" data-url="@Url.Action("GetData", "Home")">
    ...
</div>

and then in your separate js you could use the .data() method:

"sAjaxSource": $('#example').data('url')
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for suggesting helpers. If you're deploying your app within a virtual directory (e.g domain.com/myapp), using /Home/GetData won't work.
Good advice on suggestions to always use URL helpers and never hardcode urls.
0

I think your path should be

"sAjaxSource": "/home/details/home/getdata",

and shouldn't getdata be a filename like getdata.php or something

"sAjaxSource": "/home/details/home/getdata.php",

Comments

0

Did you try puting a leading slash before the Ajax Source?

"sAjaxSource": "/Home/GetData/"

UPDATE

As stated in the comments below, hardcoding the URL could cause problems later down the line.

Darin has already posted about using the built in URL helpers so I wont edit my post to include that info. I do it a slightly different way as documented here:

Create Extension methods of UrlHelper to generate your url from Route

I've found this way of working to be extremely helpful when working with a front end team. They found it really easy to understand the Url.RequestedPage() format and it meant they didn't need my help everytime they wanted to link or request something.

4 Comments

God damn it. I thought I tried every scenario obviosuly not that one! Thanks
-1, for hardcoding urls instead of using Url heleprs. This will break as soon as you deploy your application in a virtual directory inside IIS.
What @Darin said is right, although it worked for you now, it'll break in the future, use permanent healthy solutions.
Fair call Darin. I agree that using a hardcoded URL isn't the way to go.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.