我在 PUT 请求上收到 HTTP 404,如下所示:
$(function(){
$("#showMovies").click(function() {
$.ajax({
method:"GET",
url: "http://localhost:3000/movielist",
dataType: "json",
success: function (response) {
$.each(response, function(i, movie) {
const rowText = "<tr>" +
"<td>" + movie.idmovielist + "</td>" +
"<td>" + movie.name + "</td>" +
"<td>" + movie.thumbnail_path + "</td>" +
"<td>" + movie.description + "</td>" +
"<td>" + movie.year_released + "</td>" +
"<td>" + movie.language_released + "</td>" +
"<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
"<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
$("#movies").append(rowText);
});
}
});
});
$("#movieAdded").click(function() {
$.ajax({
method:"POST",
url: "http://localhost:3000/movielist/addMovie",
dataType: "json",
data: {
idmovielist: 10,
name: 'Bubble Gum',
thumnail_path: 'yourieiri.jpg',
description: 'Disturbing',
year_released: '2007',
language_released: 'french'
},
success: function (data) {
$.each(data, function(i, movie) {
const rowText = "<tr>" +
"<td>" + movie.idmovielist + "</td>" +
"<td>" + movie.name + "</td>" +
"<td>" + movie.thumbnail_path + "</td>" +
"<td>" + movie.description + "</td>" +
"<td>" + movie.year_released + "</td>" +
"<td>" + movie.language_released + "</td>" +
"<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
"<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
$("#movies").append(rowText);
});
}
});
});
$.ajax({
method:"DELETE",
url: "http://localhost:3000/movielist/8",
dataType: "json",
success: function (data) {
$.each(data, function(i, movie) {
const rowText = "<tr>" +
"<td>" + movie.idmovielist + "</td>" +
"<td>" + movie.name + "</td>" +
"<td>" + movie.thumbnail_path + "</td>" +
"<td>" + movie.description + "</td>" +
"<td>" + movie.year_released + "</td>" +
"<td>" + movie.language_released + "</td>" +
"<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
"<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
$("#movies").append(rowText);
});
}
});
$.ajax({
method:"PUT",
url: "http://localhost:3000/movielist/6",
dataType: "json",
data: {
idmovielist: 6,
name: 'Lion King',
thumanail_path: 'https://lumiere-a.akamaihd.net/v1/images/',
description: 'cartoon',
year_realeased: '2000',
language_released: 'english'
},
success: function (data) {
$.each(data, function(i, movie) {
const rowText = "<tr>" +
"<td>" + movie.idmovielist + "</td>" +
"<td>" + movie.name + "</td>" +
"<td>" + movie.thumbnail_path + "</td>" +
"<td>" + movie.description + "</td>" +
"<td>" + movie.year_released + "</td>" +
"<td>" + movie.language_released + "</td>" +
"<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
"<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
$("#movies").append(rowText);
});
}
});
});
因此,每当我尝试执行我的 put 请求时,我总是会收到状态代码 404 not found 的错误,而且我的 get post 和 delete 工作得很好,这只是我的 put 请求,我能做些什么来修复它。另外,我能够在我的网络浏览器中正常工作,因为我忘记添加功能
app.put('/movielist/id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results) => {
if (!err) {
res.send("Movie list is updated");
} else {
console.log(err);
}
});
});
还有我的帖子请求代码
app.post('/movielist/addMovie',(req, res) => {
mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
req.body,
(err,rows) => {
if (!err) {
res.send("Movie is added");
} else {
console.log(err);
}
});
});
app.get('/movielist',(req,res)=> {
mysqlConnection.query("SELECT * FROM movielist", (err, rows,fields)=> {
if (!err) {
res.send(rows);
} else {
console.log(err);
}
});
});
app.delete('/movielist/:id',(req,res) => {
mysqlConnection.query("DELETE FROM movielist WHERE idmovielist = ?",[req.params.id],(err,rows,fields) =>{
if (!err) {
res.send("Movie is deleted");
} else {
console.log(err);
}
});
});
app.post('/movielist/addMovie',(req, res) => {
mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
req.body,
(err,rows) => {
if (!err) {
res.send("Movie is added");
} else {
console.log(err);
}
});
});
app.put('/movielist/:id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results) => {
if (!err) {
res.send("Movie list is updated");
} else {
console.log(err);
}
});
});
最佳答案
我认为你需要改变这一点:
app.put('/movielist/id',(req,res) =>{
对此:
app.put('/movielist/:id',(req,res) =>{
在express框架的nodejs中,为了使用/link/参数/,参数需要用:声明。
关于jquery - 为什么我的 PUT 请求收到 404 状态错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573149/