You could reference the full path to your action in your js.
You can get the full path by using this method (http://localhost:50000/People/Add
):
@Url.Action("Add", "People", null, Request.Url.Scheme, null)
Then it will not matter which page you are on, you will always be able to call the same action, and no hassle with relative urls.
Sample:
$('#btnTest').click(function () {
// debugger;
try {
var person = { FirstName: "Joe", LastName: "Soap", Email: "a@b.c" };
var url = '@Url.Action("Add", "People", null, Request.Url.Scheme, null)';
alert("Url: " + url);
$.ajax({
url: url,
dataType: 'json',
data: person,
type: 'POST',
success: function (result) {
alert("Success!");
alert(result);
},
failure: function (result) {
alert("Fail!");
alert(result);
}
});
} catch (e) {
alert(e.responseText);
}
});