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)
                    });
                }
            });

        });
Jon
  • 38,814
  • 81
  • 233
  • 382

3 Answers3

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')
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
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.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • God damn it. I thought I tried every scenario obviosuly not that one! Thanks – Jon Jun 23 '11 at 12:31
  • 2
    -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. – Darin Dimitrov Jun 23 '11 at 12:33
  • What @Darin said is right, although it worked for you now, it'll break in the future, use permanent healthy solutions. – Ken D Jun 23 '11 at 12:40
  • 1
    Fair call Darin. I agree that using a hardcoded URL isn't the way to go. – Jamie Dixon Jun 23 '11 at 12:41
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",
T9b
  • 3,312
  • 5
  • 31
  • 50