這裏是我的代碼(以下發現的問題):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