我收到 ajax 请求的页面未找到错误这是怎么回事
I get ajax Requested page not found error what is wrong with this
当我做 POST 时,我总是得到 'Requested page not found. [404]',我不知道哪里出了问题,请指教
我有一个运行良好的 web API with ASP.NET Core MVC 项目,现在我修改它,删除数据库并添加自定义 ControllerBase
。
但是我收到 [404] 错误,缺少一些东西 ..
这是我的代码
JavaScript
const uri = "api/book";
let todos = null;
function getCount(data) {
const el = $("#counter");
let name = "to-do";
if (data) {
if (data > 1) {
name = "to-dos";
}
el.text(data + " " + name);
} else {
el.text("No " + name);
}
}
$(document).ready(function () {
getData();
});
function getData() {
$.ajax({
type: "GET",
url: uri,
cache: false,
success: function (data) {
const tBody = $("#todos");
$(tBody).empty();
getCount(data.length);
$.each(data, function (key, item) {
const tr = $("<tr></tr>")
.append(
$("<td></td>").append(
$("<input/>", {
type: "checkbox",
disabled: true,
checked: item.isComplete
})
)
)
.append($("<td></td>").text(item.name))
.append(
$("<td></td>").append(
$("<button>Edit</button>").on("click", function () {
editItem(item.id);
})
)
)
.append(
$("<td></td>").append(
$("<button>Delete</button>").on("click", function () {
deleteItem(item.id);
})
)
);
tr.appendTo(tBody);
});
todos = data;
}
});
}
function addItem() {
const item = {
name: $("#add-name").val(),
isComplete: false
};
$.ajax({
type: "POST",
accepts: "application/json",
url: uri,
contentType: "application/json",
data: JSON.stringify(item),
error: function (jqXHR, textStatus, errorThrown) {
///alert("Something went wrong!");
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
///$('#post').html(msg);
},
success: function (result) {
getData();
$("#add-name").val("");
}
});
}
function deleteItem(id) {
$.ajax({
url: uri + "/" + id,
type: "DELETE",
success: function (result) {
getData();
}
});
}
function editItem(id) {
$.each(todos, function (key, item) {
if (item.id === id) {
$("#edit-name").val(item.name);
$("#edit-id").val(item.id);
$("#edit-isComplete")[0].checked = item.isComplete;
}
});
$("#spoiler").css({ display: "block" });
}
$(".my-form").on("submit", function () {
const item = {
name: $("#edit-name").val(),
isComplete: $("#edit-isComplete").is(":checked"),
id: $("#edit-id").val()
};
$.ajax({
url: uri + "/" + $("#edit-id").val(),
type: "PUT",
accepts: "application/json",
contentType: "application/json",
data: JSON.stringify(item),
success: function (result) {
getData();
}
});
closeInput();
return false;
});
function closeInput() {
$("#spoiler").css({ display: "none" });
}
我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using WorkSampleBookSearch.Model;
namespace WorkSampleBookSearch
{
[Route("api/[controller]")]
[ApiController]
public class BooksXmlController : ControllerBase
{
public BooksXmlController()
{
}
/// <summary>
/// Retrieve all items from PhoneBook.
/// </summary>
/// <returns>PhoneBook items List</returns>
// GET: api/Book
[HttpGet]
public IActionResult GetPhoneBookItems()
{
List<PhoneBookItem> PhoneBookItems = new List<PhoneBookItem>();
XDocument doc = XDocument.Load("books.xml");
foreach (XElement element in doc.Descendants("phonebookitems")
.Descendants("phonebookitem"))
{
PhoneBookItem phonebookitem = new PhoneBookItem
{
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Publish_date
/// Description
Id = element.Element("id").Value,
Author = element.Element("author").Value,
Title = element.Element("title").Value,
Genre = element.Element("genre").Value,
Price = element.Element("price").Value,
Publish_date = element.Element("publish_date").Value,
Description = element.Element("description").Value
};
PhoneBookItems.Add(phonebookitem);
PhoneBookItems = PhoneBookItems.OrderBy(p => p.Title).ToList();
}
return Ok(PhoneBookItems);
}
/// <summary>
/// Returns a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <returns>PhoneBook item</returns>
// GET: api/Book/5
[HttpGet("{id}")]
public IActionResult GetPhoneBookItem(long id)
{
XDocument doc = XDocument.Load("books.xml");
XElement element = doc.Element("phonebookitems").Elements("phonebookitem").
Elements("id").SingleOrDefault(x => x.Value == id.ToString());
XElement parent = element.Parent;
PhoneBookItem phonebookitem = new PhoneBookItem
{
Id = element.Element("id").Value,
Author = element.Element("author").Value,
Title = element.Element("title").Value,
Genre = element.Element("genre").Value,
Price = element.Element("price").Value,
Publish_date = element.Element("publish_date").Value,
Description = element.Element("description").Value
};
return Ok(phonebookitem);
}
/// <summary>
/// Insert a PhoneBook item.
/// </summary>
/// <returns>Inserts new PhoneBook item in books.xml and saves the file</returns>
//POST: api/Book
[HttpPost]
public void PostPhoneBookItem(PhoneBookItem PhoneBookItem)
{
int maxId;
XDocument doc = XDocument.Load("books.xml");
bool elementExist = doc.Descendants("phonebookitem").Any();
if (elementExist)
{
maxId = doc.Descendants("phonebookitem").Max(x => (int)x.Element("id"));
}
else
{
maxId = 0;
}
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Price
/// Publish_date
/// Description
XElement root = new XElement("phonebookitem");
root.Add(new XElement("id", maxId + 1));
root.Add(new XElement("author", PhoneBookItem.Author));
root.Add(new XElement("title", PhoneBookItem.Title));
root.Add(new XElement("genre", PhoneBookItem.Genre));
root.Add(new XElement("price", PhoneBookItem.Price));
root.Add(new XElement("publish_date", PhoneBookItem.Publish_date));
root.Add(new XElement("description", PhoneBookItem.Description));
doc.Element("phonebookitems").Add(root);
doc.Save("books.xml");
}
/// <summary>
/// Updates a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <param name="PhoneBookItem">Retrieved PhoneBook item</param>
/// <returns>Updates PhoneBook item in books.xml and saves the file</returns>
//PUT: api/Book/5
[HttpPut("{id}")]
public void PostPhoneBookItem(long id, PhoneBookItem PhoneBookItem)
{
XDocument doc = XDocument.Load("books.xml");
var items = from item in doc.Descendants("phonebookitem")
where item.Element("id").Value == id.ToString()
select item;
foreach (XElement itemElement in items)
{
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Publish_date
/// Description
itemElement.SetElementValue("author", PhoneBookItem.Author);
itemElement.SetElementValue("title", PhoneBookItem.Title);
itemElement.SetElementValue("genre", PhoneBookItem.Genre);
itemElement.SetElementValue("price", PhoneBookItem.Price);
itemElement.SetElementValue("publish_date", PhoneBookItem.Publish_date);
itemElement.SetElementValue("description", PhoneBookItem.Description);
}
doc.Save("books.xml");
}
/// <summary>
/// Delete a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <returns>Deletes item from books.xml and saves the file</returns>
// DELETE: api/Book/5
[HttpDelete("{id}")]
public void DeletePhoneBookItem(long id)
{
XDocument doc = XDocument.Load("books.xml");
var elementToDelete = from item in doc.Descendants("phonebookitem")
where item.Element("id").Value == id.ToString()
select item;
elementToDelete.Remove();
doc.Save("books.xml");
}
}
}
这是我的创业公司
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace WorkSampleBookSearch
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
更改 URI 来自
const uri = "api/book";
至
const uri = "api/booksxml";
说明:
[Route("api/[controller]")]
表示路由将是 api/controllerName
,在您的情况下,控制器的名称是 BooksXml
(不考虑 Controller
后缀)。
当我做 POST 时,我总是得到 'Requested page not found. [404]',我不知道哪里出了问题,请指教
我有一个运行良好的 web API with ASP.NET Core MVC 项目,现在我修改它,删除数据库并添加自定义 ControllerBase
。
但是我收到 [404] 错误,缺少一些东西 ..
这是我的代码
JavaScript
const uri = "api/book";
let todos = null;
function getCount(data) {
const el = $("#counter");
let name = "to-do";
if (data) {
if (data > 1) {
name = "to-dos";
}
el.text(data + " " + name);
} else {
el.text("No " + name);
}
}
$(document).ready(function () {
getData();
});
function getData() {
$.ajax({
type: "GET",
url: uri,
cache: false,
success: function (data) {
const tBody = $("#todos");
$(tBody).empty();
getCount(data.length);
$.each(data, function (key, item) {
const tr = $("<tr></tr>")
.append(
$("<td></td>").append(
$("<input/>", {
type: "checkbox",
disabled: true,
checked: item.isComplete
})
)
)
.append($("<td></td>").text(item.name))
.append(
$("<td></td>").append(
$("<button>Edit</button>").on("click", function () {
editItem(item.id);
})
)
)
.append(
$("<td></td>").append(
$("<button>Delete</button>").on("click", function () {
deleteItem(item.id);
})
)
);
tr.appendTo(tBody);
});
todos = data;
}
});
}
function addItem() {
const item = {
name: $("#add-name").val(),
isComplete: false
};
$.ajax({
type: "POST",
accepts: "application/json",
url: uri,
contentType: "application/json",
data: JSON.stringify(item),
error: function (jqXHR, textStatus, errorThrown) {
///alert("Something went wrong!");
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
alert(msg);
///$('#post').html(msg);
},
success: function (result) {
getData();
$("#add-name").val("");
}
});
}
function deleteItem(id) {
$.ajax({
url: uri + "/" + id,
type: "DELETE",
success: function (result) {
getData();
}
});
}
function editItem(id) {
$.each(todos, function (key, item) {
if (item.id === id) {
$("#edit-name").val(item.name);
$("#edit-id").val(item.id);
$("#edit-isComplete")[0].checked = item.isComplete;
}
});
$("#spoiler").css({ display: "block" });
}
$(".my-form").on("submit", function () {
const item = {
name: $("#edit-name").val(),
isComplete: $("#edit-isComplete").is(":checked"),
id: $("#edit-id").val()
};
$.ajax({
url: uri + "/" + $("#edit-id").val(),
type: "PUT",
accepts: "application/json",
contentType: "application/json",
data: JSON.stringify(item),
success: function (result) {
getData();
}
});
closeInput();
return false;
});
function closeInput() {
$("#spoiler").css({ display: "none" });
}
我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using WorkSampleBookSearch.Model;
namespace WorkSampleBookSearch
{
[Route("api/[controller]")]
[ApiController]
public class BooksXmlController : ControllerBase
{
public BooksXmlController()
{
}
/// <summary>
/// Retrieve all items from PhoneBook.
/// </summary>
/// <returns>PhoneBook items List</returns>
// GET: api/Book
[HttpGet]
public IActionResult GetPhoneBookItems()
{
List<PhoneBookItem> PhoneBookItems = new List<PhoneBookItem>();
XDocument doc = XDocument.Load("books.xml");
foreach (XElement element in doc.Descendants("phonebookitems")
.Descendants("phonebookitem"))
{
PhoneBookItem phonebookitem = new PhoneBookItem
{
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Publish_date
/// Description
Id = element.Element("id").Value,
Author = element.Element("author").Value,
Title = element.Element("title").Value,
Genre = element.Element("genre").Value,
Price = element.Element("price").Value,
Publish_date = element.Element("publish_date").Value,
Description = element.Element("description").Value
};
PhoneBookItems.Add(phonebookitem);
PhoneBookItems = PhoneBookItems.OrderBy(p => p.Title).ToList();
}
return Ok(PhoneBookItems);
}
/// <summary>
/// Returns a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <returns>PhoneBook item</returns>
// GET: api/Book/5
[HttpGet("{id}")]
public IActionResult GetPhoneBookItem(long id)
{
XDocument doc = XDocument.Load("books.xml");
XElement element = doc.Element("phonebookitems").Elements("phonebookitem").
Elements("id").SingleOrDefault(x => x.Value == id.ToString());
XElement parent = element.Parent;
PhoneBookItem phonebookitem = new PhoneBookItem
{
Id = element.Element("id").Value,
Author = element.Element("author").Value,
Title = element.Element("title").Value,
Genre = element.Element("genre").Value,
Price = element.Element("price").Value,
Publish_date = element.Element("publish_date").Value,
Description = element.Element("description").Value
};
return Ok(phonebookitem);
}
/// <summary>
/// Insert a PhoneBook item.
/// </summary>
/// <returns>Inserts new PhoneBook item in books.xml and saves the file</returns>
//POST: api/Book
[HttpPost]
public void PostPhoneBookItem(PhoneBookItem PhoneBookItem)
{
int maxId;
XDocument doc = XDocument.Load("books.xml");
bool elementExist = doc.Descendants("phonebookitem").Any();
if (elementExist)
{
maxId = doc.Descendants("phonebookitem").Max(x => (int)x.Element("id"));
}
else
{
maxId = 0;
}
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Price
/// Publish_date
/// Description
XElement root = new XElement("phonebookitem");
root.Add(new XElement("id", maxId + 1));
root.Add(new XElement("author", PhoneBookItem.Author));
root.Add(new XElement("title", PhoneBookItem.Title));
root.Add(new XElement("genre", PhoneBookItem.Genre));
root.Add(new XElement("price", PhoneBookItem.Price));
root.Add(new XElement("publish_date", PhoneBookItem.Publish_date));
root.Add(new XElement("description", PhoneBookItem.Description));
doc.Element("phonebookitems").Add(root);
doc.Save("books.xml");
}
/// <summary>
/// Updates a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <param name="PhoneBookItem">Retrieved PhoneBook item</param>
/// <returns>Updates PhoneBook item in books.xml and saves the file</returns>
//PUT: api/Book/5
[HttpPut("{id}")]
public void PostPhoneBookItem(long id, PhoneBookItem PhoneBookItem)
{
XDocument doc = XDocument.Load("books.xml");
var items = from item in doc.Descendants("phonebookitem")
where item.Element("id").Value == id.ToString()
select item;
foreach (XElement itemElement in items)
{
/// Id
/// Author
/// Title
/// Genre
/// Price
/// Publish_date
/// Description
itemElement.SetElementValue("author", PhoneBookItem.Author);
itemElement.SetElementValue("title", PhoneBookItem.Title);
itemElement.SetElementValue("genre", PhoneBookItem.Genre);
itemElement.SetElementValue("price", PhoneBookItem.Price);
itemElement.SetElementValue("publish_date", PhoneBookItem.Publish_date);
itemElement.SetElementValue("description", PhoneBookItem.Description);
}
doc.Save("books.xml");
}
/// <summary>
/// Delete a PhoneBook item matching the given id.
/// </summary>
/// <param name="id">Id of item to be retrieved</param>
/// <returns>Deletes item from books.xml and saves the file</returns>
// DELETE: api/Book/5
[HttpDelete("{id}")]
public void DeletePhoneBookItem(long id)
{
XDocument doc = XDocument.Load("books.xml");
var elementToDelete = from item in doc.Descendants("phonebookitem")
where item.Element("id").Value == id.ToString()
select item;
elementToDelete.Remove();
doc.Save("books.xml");
}
}
}
这是我的创业公司
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace WorkSampleBookSearch
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
更改 URI 来自
const uri = "api/book";
至
const uri = "api/booksxml";
说明:
[Route("api/[controller]")]
表示路由将是 api/controllerName
,在您的情况下,控制器的名称是 BooksXml
(不考虑 Controller
后缀)。