Using POST in jQuery with ASP.NET MVC
I the site is on the follo开发者_Go百科wing URL localhost:xxxx/VnosPrijavnica/Predmeti/123456
, where the VnosPrijavnica
is the controller, Predmeti
is an action in the controller, and 123456
is an optional param (everything is defined correctly in Global.asax). The site loads fine, just when it comes to jQuery's $.post()
I encoutered a problem. The jQuery code looks like this:
$("#predmet").change(function () {
$("#predmet option[value='prazen']").hide();
$.post("/VnosPrijavnica/PoisciRoke", $("#kriteriji").serialize(), function (data) {
$("#roki").html(data);
});
});
The $.post()
never executes. I noticed if I change the URL to localhost:xxxx/VnosPrijavnica
this piece of jQuery code works as intended. The question is what must I modify in the jQuery code that it will work with the first URL as it does in the second?
Try something like this:
var baseUrl = '<%= ResolveUrl("~/") %>';
$("#predmet").change(function () {
$("#predmet option[value='prazen']").hide();
$.post(baseUrl + "/VnosPrijavnica/PoisciRoke", $("#kriteriji").serialize(), function (data) {
$("#roki").html(data);
});
});
精彩评论