JQuery Ajax Url

I am doing an Ajax call from

i wanna access the url but when i check in fireBug it is trying to access

here is my code

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

5 Answers

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.

2

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=""></script>
 $.ajax({
        url:location.origin + "/account/deleteComment/" + deleteID,
        success: function () {
            $("#comment-" + deleteID).slideUp("fast");
        }
    });

Change the URL to:

/account/deleteComment/

That way it'll go to the root path:


Absolute URL is a good idea in ajax but this is more good and easy way. Just declared var siteURL = 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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest