Saturday, July 9, 2011

Jquery Post URL problems in IIS hosted Eenvironments

If you have used JQuery post with ASP.Net MVC the URL in the post will be something like
"YourControllerName/ActionName"
Example:-
$.post("Home/CreatePerson", { name: "John", time: "2pm" },
    function(data) {
    alert(data);
});
This will work great until you deploy your project in IIS with an alias to your web application. Let say the give alias is "MyWeb". The the URL will be -your-iis-server-ip/MyWeb. But the Jquery post in above will post its data to a URL something like -your-iis-server-ip/Home/CreatePerson

To avoid this you need to create post URL dynamically using @Url.Action() helper. Here is the corrected JQuery post.

$.post("@Url.Action("CreatePerson", "Home")", { name: "John", time: "2pm" },
    function(data) {
    alert(data);
});

No comments:

Post a Comment