5

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?

2 Answers 2

10

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

$.ajax({
    type: "POST",
    url: "<%= Url.Action("GetDetails") %>",
    ......
});
Sign up to request clarification or add additional context in comments.

1 Comment

Mostly right, but here it should be Url.Action, not Url.Content.
5

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

url: "/Home/GetDetails"

3 Comments

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).
What do you do when you have a separate js file with all the javascript in there?
I tend to hardcode (gasp!) the URLs. If I have the time, I might have a JS file generated on the server with the URLs in that assigned to the approprite JavaScript variables, but I'm not pragmatic about that.

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.