如何通过问号快递路由器api更新sql

问题描述 投票:0回答:1

我想使用和End-Point Api(Express NodeJS)更新varchar字段(字符串),但是我有问题,我传递了诸如问号之类的无效输入。

快速端点:

router.get("/updateField/:table/:field/:value/:num/:postid/", function(req, res) {

    connection.query(

        'UPDATE '+ req.params.table +' SET ' + req.params.field +' = '+JSON.stringify(req.params.value) +' where language ='+ req.params.num +' and post_id ='+req.params.postid 

此代码可以正常工作:

http://localhost:3001/api/updateField/posts/TITLE/When/1/1

但是这不起作用:

http://localhost:3001/api/updateField/posts/TITLE/When?/1/1

我从响应中发送请求是这样的:

fetch(
      "http://localhost:3001/api/updateField/" +
        table +
        "/" +
        field +
        "/" +
        value +
        "/" +
        lenguage +
        "/" +
        post_id
    );
javascript sql reactjs express
1个回答
0
投票

使用javascript函数encodeURIComponent()来转义URL参数中的特殊字符。

例如,在浏览器控制台上尝试一下,您将获得一个主意:

console.log(
      "http://localhost:3001/api/updateField/" +
        table +
        "/" +
        field +
        "/" +
        encodeURIComponent(value) +
        "/" +
        lenguage +
        "/" +
        post_id
    );
console.log(encodeURIComponent("When?"));

您将在URL中看到“何时?”被替换为“何时%3F”。在Node.Js中,您将以字符串“ When?”

的形式接收参数值。

要了解有关encodeURIComponent()的更多信息,请参阅this

© www.soinside.com 2019 - 2024. All rights reserved.