gpt4 book ai didi

javascript - Node js 路由器不处理删除

转载 作者:行者123 更新时间:2023-12-01 03:23:11 25 4
gpt4 key购买 nike

我的node js路由器它不处理删除方法,但它处理得很好(GET、POST、UPDATE等)的所有其他方法,但删除没有到达路由器,我不明白为什么。

ajax 正确地执行了请求,至少看起来是这样。

ajax请求:

function ajaxHelper(url, onSuccessArray, onErrorArray, onFinishArray, verb, data) {
verb = verb || "GET";
document.getElementById("loader").style.display = "block";
let xhr = new XMLHttpRequest();
xhr.open(verb, url);
xhr.responseType = "json";
xhr.addEventListener("load", function () {
document.getElementById("loader").style.display = "none";
let status = this.status;
if (status === 200) {

if (onSuccessArray) {

onSuccessArray.reverse();
onSuccessArray.forEach(function (onSuccess) {

onSuccess && onSuccess(xhr.response)
});
}
} else {

if (onErrorArray) {

onErrorArray.reverse();
onErrorArray.forEach(function (onErrorCallback) {

onErrorCallback && onErrorCallback(status, xhr.response);
})
}
}
if (onFinishArray) {

onFinishArray.reverse();
onFinishArray.forEach(function (onFinishCallback) {

onFinishCallback && onFinishCallback(status);
})
}
});
if (data) {

xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(data));
} else {

xhr.send();
}
}

function removePlayer(onSuccessArray, onErrorArray, onFinishArray, data) {

ajaxHelper("/players/delete-player", onSuccessArray, onErrorArray, onFinishArray, "DELETE", data);
}

app.js

let express = require('express');
let path = require('path');
let favicon = require('serve-favicon');
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');

let index = require('./routes/index');
let cups = require('./routes/route-cup');
let leagues = require('./routes/route-league');
let players = require('./routes/route-player');
let teams = require('./routes/route-team');

let app = express();

app.use(favicon(path.join(__dirname, 'public', '/images/favicon.png')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());


app.use('/cups', cups);
app.use('/leagues', leagues);
app.use('/players', players);
app.use('/teams', teams);
app.use(express.static("public", {
"index": "index.html"
}));

// catch 404 and forward to error handler
app.use(function (req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;

路由器:

let express = require('express');
let router = express.Router();
let mysql = require("mysql");
let sqlHelper = require("../scripts/sql-helper");

router.get('/', function (req, res, next) {
let sqlQuery = "SELECT * FROM " + dbName + ".player;";
sqlHelper.executeSql(res, sqlQuery, function (rows, fields) {

res.json(rows);
})
});

router.get('/free-players', function (req, res, next) {
let sqlQuery = "SELECT * FROM " + dbName + ".player WHERE 'teamName' <> ''";
sqlHelper.executeSql(res, sqlQuery, function (rows, fields) {

res.json(rows);
})
});

router.post('/submit-player', function (req, res, next) {
let keys = objectHelper.getObjectKeys(req.body);
let values = objectHelper.getObjectValues(req.body);
let placeHolders = new Array(keys.length).fill("?");
let sqlQuery = "INSERT INTO " + dbName + ".`player` (" + keys.toString() + ") VALUES (" + placeHolders.toString() + ")";

sqlQuery = mysql.format(sqlQuery, values);
sqlHelper.executeSql(res, sqlQuery, function (rows, fields) {

res.json(rows);
});
});

router.put('/update-player', function (req, res, next) {
let keys = objectHelper.getObjectKeys(req.body);
let values = objectHelper.getObjectValues(req.body);
let sqlQuery = "UPDATE " + dbName + ".`player` SET ";
let valuePair = [];
keys.forEach(function (key, index) {
if (values[index] !== null) {

valuePair.push(key + "=" + "'" + values[index] + "'");
} else {

valuePair.push(key + "=" + values[index]);
}
});
sqlQuery = sqlQuery + valuePair.toString() + " WHERE " + valuePair[0];
sqlHelper.executeSql(res, sqlQuery, function (rows, fields) {

res.json(rows);
});
});

router.delete('/delete-player', function (req, res, next) {
let sqlQuery = "DELETE " + dbName + ".`player` WHERE id=" + req.body;
sqlHelper.executeSql(res, sqlQuery, function (rows, fields) {

res.json(rows);
});
});

module.exports = router;

服务器日志:

GET /players 304 53.636 ms - -
GET /teams 304 46.161 ms - -
GET /players 304 13.676 ms - -
DELETE /players/delete-player 400 3059.064 ms - 1397
Error: No default engine was specified and no extension was provided.

最佳答案

您收到 400 响应(“错误请求”),因为您很可能向后端发送(JSON 编码的)字符串。 p>

但是,当使用 application/json 时,您发送的数据应该是(JSON 编码的)对象或数组,但不是字符串。这就是服务器返回 400 的原因。

更典型的解决方案是在 URL 本身中传递要删除的属性:

DELETE /players/delete-player/jack

您将使用这样的路线:

router.delete('/delete-player/:name', ...)

并以 req.params.name 的形式访问名称(在本例中等于“jack”)。

有关路线参数的更多信息:http://expressjs.com/en/guide/routing.html#route-parameters

关于javascript - Node js 路由器不处理删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45012808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com