11

I am doing an Ajax call from site.com/users/{username}

i wanna access the url site.com/account/deleteComment but when i check in fireBug it is trying to access site.com/users/account/deleteComment

here is my code

    $.ajax({
        url: "../account/deleteComment/" + deleteID,
        success: function () {
            $("#comment-" + deleteID).slideUp("fast");
        }
    });
0

5 Answers 5

17

Well, then ../../ is going to do the trick, isn't it?

That said, it would probably be a good idea to use absolute URLs here.

  url: "/account/deleteComment/" + deleteID,

this will take away your ability to easily move your application into a sub-folder, but in most cases, that is not a problem.

Sign up to request clarification or add additional context in comments.

2 Comments

looks like it works. on local host i am having my app at localhost/project/ so its going to localhost/account/deleteComment is there a way to make it work from any place. i am sure it will work on the server :D
@Harsha you would have to define the current subdirectory of the application in a central .js file: rootPath = "http://localhost/project" and add rootPath to every URL you specify.
11

you can use location.origin for get base address

var deleteID;
 $.ajax({
        url:location.origin + "/account/deleteComment/" + deleteID,
        success: function () {
            $("#comment-" + deleteID).slideUp("fast");
        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 $.ajax({
        url:location.origin + "/account/deleteComment/" + deleteID,
        success: function () {
            $("#comment-" + deleteID).slideUp("fast");
        }
    });

1 Comment

Put me on the right path where I found window.location.origin developer.mozilla.org/en-US/docs/Web/API/Location/origin
2

Change the URL to:

/account/deleteComment/

That way it'll go to the root path:

site.com/account/deleteComment

Comments

2

Absolute URL is a good idea in ajax but this is more good and easy way. Just declared var siteURL = www.example.com; globally. And used this in every ajax request like below.

<script>
$.ajax({
    url: siteURL + '/path/to/file',
    type: 'POST',
    data: {param1: 'value1'},
});
</script>

Generally, I declared in main index file or config of JS file.

Comments

1

I've just had a similar problem in one of my apps.
The solution I used was

$.url("Config/GetEnvironment")

Which ever directory I am in the URL is rendered correctly.

Comments

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.