ASP.Net MVC routing issue with jQuery AJAX

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

In my jQuery AJAX call, I have the following, however, when it calls this call to domain.com/home/details/home/getdata p>

What can I do to solve it correctly?

$(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
jquery c # asp.net-mvc asp.net-mvc-3 routing
source share
3 answers

Absolutely always use the URLs of helpers when working with URLs in ASP.NET MVC. Absolutely never a hardcode URL like you did.

So:

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

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

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

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

 "sAjaxSource": $('#example').data('url') 
+17
source share

Have you tried putting the main slash in front of Ajax Source?

 "sAjaxSource": "/Home/GetData/" 

UPDATE

As pointed out in the comments below, hard-coded URLs can cause problems later in the line.

Darin has already posted on the use of the built-in URL helpers, so I will not edit my post to include this information. I do this a little differently, as described here:

Create UrlHelper extension methods to generate your URL from a route

I found this way of working extremely useful when working with a development team. It was very easy for them to understand the format of Url.RequestedPage() , and this meant that they did not need my help every time they wanted to bind or request something.

0
source share

I think your path should be

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

and should not getdata be a file name like getdata.php or something

 "sAjaxSource": "/home/details/home/getdata.php", 
0
source share

All Articles