这里是我的代码(以下发现的问题):ASP.NET MVC 3 AJAX请求返回404 Not Found错误
VIEW
// This function is called by another function when radioButtonGroup.change().
var requestValues = function (form) {
var option = form.find("input:radio:checked").attr("value");
// This seemingly shows the correct url for the action method desired.
alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);
if (form.valid()) {
$.ajax({
url: form[0].action,
type: form[0].method,
data: option,
success: function (result) {
alert("Had success.");
$('#createForm').replaceWith(result);
},
error: function (xhr) {
alert("An error occurred: " + xhr.status + " " + xhr.statusText);
}
});
}
return false;
}
...(other code here)...
@using (Html.BeginForm("CreateForm", "MyController", FormMethod.Post,
new { @id = "optionForm" }))
{
<div id="options">
@foreach (MyOption op in Model.GetOptions()) {
<div class="editor-field">
@Html.RadioButton("formOption", op.OptionType, false,
new { @id = op.ID, @title = @op.Description })
<label for="@op.ID">@op.Name</label>
</div>
}
</div>
<input type="submit" value="Select" style="display:none;" />
}
控制器
[HttpPost]
public PartialViewResult CreateForm(MyOptionType formOption) {
MyViewModel model = new MyViewModel();
model.ApplyOptionValues(formOption);
return PartialView("_CreateForm", model);
}
REGISTER ROUTES
// Default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我的问题是,当我点击一个单选按钮,执行AJAX请求,但我得到了“404 Not Found”错误(尽管jQuery函数中的alert
似乎显示了适当的url)。我昨天花了整整一天的时间,我无法弄清楚什么是错误的。我在IIS Express上运行ASP.NET MVC 3应用程序,并且我没有使用区域(我知道的)。任何人有任何建议如何解决这个问题?谢谢。
EDIT
警报框显示以下消息:
Form Action:
https://localhost:44300/MyController/CreateForm
Form Method: post
EDIT
这里是,再现误差的整个测试图和测试控制器:
VIEW
<h2>TestAction</h2>
<script type="text/javascript">
$(document).ready(function() {
$("#optionForm input[name='radioOption']").change(function() {
requestValues($(this).closest("form"));
});
var requestValues = function (form) {
var option = form.find("input:radio:checked").attr("value");
alert("Form Action: " + form[0].action + "\nForm Method: " + form[0].method);
if (form.valid()) {
$.ajax({
url: form[0].action,
type: form[0].method,
data: option,
success: function (result) {
alert("AJAX success.");
//$('#createForm').replaceWith(result);
},
error: function (xhr) {
alert("An error occurred: " + xhr.status + " " + xhr.statusText);
}
});
}
return false;
}
});
</script>
@using (Html.BeginForm("CreateForm", "Test", FormMethod.Post, new { @id = "optionForm" })) {
@Html.RadioButton("radioOption", "value1", false, new { @id = "radioButton1" })
<label for="radioButton1">Radio Button 1</label>
@Html.RadioButton("radioOption", "value2", false, new { @id = "radioButton2" })
<label for="radioButton2">Radio Button 2</label>
@Html.RadioButton("radioOption", "value3", false, new { @id = "radioButton3" })
<label for="radioButton3">Radio Button 3</label>
<input type="submit" value="Select" style="display:none;" />
}
<div id="createForm"></div>
控制器
public class TestController : Controller {
public ActionResult TestAction() {
return View();
}
[HttpPost]
public ActionResult CreateForm(string option) {
return View("TestAction");
}
}
建议您使用诸如Fiddler工具,或Chrome中的网络工具(Ctrl Shift I) - 404将以红色突出显示,您可以看到有问题的URL秒。 – StuartLC 2012-08-14 08:12:30
MyController是控制器的真实名称吗?你能否给我们更多的细节来尝试再现你的问题? – 2012-08-14 08:13:47
你可以发布警报中显示的内容吗? – nemesv 2012-08-14 08:19:30