开发者

jQuery Ajax URL

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 s开发者_开发技巧ite.com/users/account/deleteComment

here is my code

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


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.


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");
        }
    });


Change the URL to:

/account/deleteComment/

That way it'll go to the root path:

site.com/account/deleteComment


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜