views:

139

answers:

2

Hi.

I have wrong URL in my ajax calls.

$.ajax({
    type: "POST",
    url: "Home/GetDetails",
    ......
});

HomeController has action GetDetails().

All works fine, when I load page with URL htp://localhost/projectName Ajax URL is htp://localhost/projectName/Home/GetDetails

But after loading htp://localhost/projectName/Home/Index all my ajax calls are going to htp://localhost/projectName/Home/Home/GetDetails and thats wrong.

Please, how can I solve this?

+7  A: 

You should use the Url Helper to generate your URLs...

$.ajax({
    type: "POST",
    url: "<%= Url.Action("GetDetails") %>",
    ......
});
Kieron
Mostly right, but here it should be Url.Action, not Url.Content.
Craig Stuntz
Whoops, fixed - thanks @Craig.
Kieron
works fine :) thank you
coosha
A: 

If you stick with the strings and not Url.Action, put a forward slash before 'Home'

url: "/Home/GetDetails"
swilliams
This assumes that your application is always hosted at the root of the web site. If you have multiple applications hosted then it's most likely that each application has its own virtual directory. You can either hard-code the entire URL, including the virtual directory, or use the URL helpers in MVC to generate the URLs dynamically (which should always work).
Eilon