【发布时间】:2017-01-18 06:35:23
【问题描述】:
尝试使用 Ajax 向 Home 控制器发送字符串值。以下 Ajax 调用给出了错误(在 Google Chrome 开发者工具中):Failed to load resource: the server responded with a status of 404 (Not Found):
$.ajax({
url: 'Home/TestAction',
data: "Test data",
type: 'POST',
success: function (obj) {
alert('Suceeded');
}
});
更新:
之后我已经尝试了这篇文章的所有回复中的 url 部分(截至目前),结果证明 url 是正确的,但我仍然得到一个 warning 和一个error 如下图所示。 注意:图片中的控制器名称和动作名称不同,但它们在 VS2015 中名为 Controllers 的同一个默认文件夹中。我没有创建任何子文件夹等。另外,这是在开发机器Win10 with VS2015 ASP.NET Core 1.1, IIS EXPRESS 上。请点击图片以清晰地看到消息。
更新 2:
为了让我的问题更容易理解,我在official ASP.NET MVC Core tutorial 之后创建了一个测试 ASP.NET Core 应用程序。请看下面的错误图片。
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,Genre,Price,ReleaseDate,Title")] Movie movie)
{
if (id != movie.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(movie);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!MovieExists(movie.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("Index");
}
return View(movie);
}
查看 [..\views\movies\Index.cshtml]。注意:我只添加了一个额外的按钮 <button type="button" id="testid" class="Savebtn">Test</button>
和最后的 Ajax 调用。
@model MovieGenreViewModel
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="Movies" asp-action="Index" method="get">
<p>
<select asp-for="movieGenre" asp-items="Model.genres">
<option value="">All</option>
</select>
Title: <input type="text" name="SearchString">
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.movies[0].Genre)
</th>
<th>
@Html.DisplayNameFor(model => model.movies[0].Price)
</th>
<th>
@Html.DisplayNameFor(model => model.movies[0].ReleaseDate)
</th>
<th>
@Html.DisplayNameFor(model => model.movies[0].Title)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.movies) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
<button type="button" id="testid" class="Savebtn">Test</button>
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
</td>
</tr>
}
</tbody>
</table>
@section scripts
{
<script>
$(document).ready(function () {
$('.Savebtn').click(function () {
$.ajax({
url: '@Url.Action("Edit", "Movies")',
data: "Test data",
type: 'POST', //POST if you want to save, GET if you want to fetch data from server
success: function (obj) {
// here comes your response after calling the server
alert('Suceeded');
},
error: function (obj) {
alert('Something happened');
}
});
});
});
</script>
}
错误 [在 Google Chrome 开发者工具中]:
【问题讨论】:
-
url: '@Url.Action("TestAction", "Home")',- 始终使用Url.Action()正确生成您的网址 -
请出示您的控制器代码
-
@StephenMuecke 我仍然遇到错误。我在帖子中添加了 UPDATE 部分。
-
@HaithamShaddad 我添加了一个 UPDATE 2 部分,其中包含您和其他人要求的更多详细信息。
-
@nam,问题中没有答案,我已经回滚了您的更改。如果现有答案不能解决问题,请随意添加您自己的答案。将来,当原始问题已解决时,不要仅仅因为您遇到不同的错误而继续编辑您的问题(您需要提出一个新问题)。作为旁注
return RedirectToAction("Index");在您的 PST 方法中毫无意义 - ajax 调用 never 重定向 - 使用它们的全部意义在于保持在同一页面上
标签: ajax visual-studio-2015 asp.net-core asp.net-core-mvc