I have a jQuery script like below.
$.ajax({
type: "post",
url: "/TestPage/Parameter",
data: JSON.stringify(formData),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data, textStatus, jqXHR) {
alert('created!');
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
alert(err);
}
});
My website has a default page www.example.com which is the same as www.example.com/Home/Index.
When I go to www.example.com and trigger the jQuery it works fine. The website sends a request to www.example.com/TestPage/Parameter but when I go to www.example.com/Home/Index and trigger the script, it makes a request to www.example.com/Home/Index/TestPage/Parameter which does not exist.
Is there a way to fix this problem?
@Url.Actionhelper in your markup, then parse the form attributes in your jquery. Doing this can make your jquery for posting more generic and reusable too.urlrootwith the value ofUrl.Content("~/"). This way I always have a safe URL root reference.