petko_stankoski
petko_stankoski

Reputation: 10713

Ajax calls wrong method

Here is my method:

function logOff() {
    $.ajax({ url: "Login/LogOff", type: "GET", success: function (data) { window.location.href = "Login/Index"; } })//end of ajax call
}

With this, I want the action method LogOff in the LoginController to be called. However, what is called is: http://localhost:6355/Home/Login/LogOff and I get error. Why is this happening?

Upvotes: 1

Views: 289

Answers (2)

petko_stankoski
petko_stankoski

Reputation: 10713

Here is what I did:

if (data == "") {
    patharray = window.location.href.split('/');
    data = window.location.protocol + "//" + patharray[2];
}
window.location.href = data;

Upvotes: 0

VisioN
VisioN

Reputation: 145368

You have to put one extra slash in the begining of your url value. This is called relative URL.

function logOff() {
    $.ajax({
        url: "/Login/LogOff", // <-- slash before "Login"
        type: "GET",
        success: function (data) {
            window.location.href = "/Login/Index"; // <-- slash before "Login"
        }
    })
}

Another option is to use absolute URL:

url: "http://localhost:6355/Login/LogOff"

But it is not flexible.

Upvotes: 1

Related Questions