Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript decodeUri不适用于res.json express_Javascript_Json_Node.js_Express - Fatal编程技术网

Javascript decodeUri不适用于res.json express

Javascript decodeUri不适用于res.json express,javascript,json,node.js,express,Javascript,Json,Node.js,Express,有一个express应用程序可以将经过清理的url保存到mongodb数据库,我想使用decodeURI()在res.json中呈现解码后的url,但它无法按预期工作,只返回编码版本。如果我使用res.send(decodeURI(url))它就可以工作。如何让res.json发送解码的url // Create a url object with escaped and trimmed data. var Url = new UrlModel( { url:

有一个express应用程序可以将经过清理的url保存到mongodb数据库,我想使用decodeURI()在res.json中呈现解码后的url,但它无法按预期工作,只返回编码版本。如果我使用res.send(decodeURI(url))它就可以工作。如何让res.json发送解码的url

        // Create a url object with escaped and trimmed data.
    var Url = new UrlModel(
      { url: req.body.url }
    );


    if (!errors.isEmpty()) {
        // There are errors. Render the form again with error messages.
        res.render('index', { errors: errors.array()});
    return;
    }
    else {
        // Data from form is valid.
        // Check if Url with same name already exists.
        UrlModel.findOne({ 'url': req.body.url })
            .exec( function(err, found_url) {
                 if (err) { return next(err); }

                 if (found_url) {
                     // Url exists, redirect to its detail page.
                     res.json({"original_url": decodeURI(found_url.url) });
                     //res.send(decodeURI(found_url.url))

                 }

更新:

我的问题可能不清楚。我的输入来自mongodb,表单中有一个经过清理的url

https://www.facebook.com
所以我想转换的是html实体,我不认为decodeUri会这样做。 我的名字是从这个代码中出来的

res.json({original\u url:found\u url.url,decoded:decodeURI(found\u url.url)})

{“原始url”:“https:/;/;www.facebook.com”,“解码”:“https:/;/;www.facebook.com”}


因此,
//未转换为//。是否有一些核心javascript函数可以做到这一点,或者我必须使用带有regx的函数并替换它?

在问题更新后更新。

在JavaScript中,您可以使用一些函数来完成类似的转换:
encodeURI
encodeURIComponent
,以及它们对应的
decodeURI
decodeURIComponent
encodeURI
用于安全地对完整URL进行编码,因为它不会对协议、主机名或路径进行编码<代码>编码组件
将对所有内容进行编码

你在编辑的问题中展示的内容与JavaScript无关(据我所知);在将字符串发送回您之前,您需要让后端取消初始化该字符串

如果无法更新后端,您可以尝试以下操作:

unescape('https:&#x2F;&#x2F;www.facebook.com'.replace(/&#x/g, '%').replace(/;/g, ''))
这将把这些实体解码成它们的实际字符,但它不应该是一个永久的解决方案


原始响应。

我对encodeURI和decodeURI没有任何问题。您是否完全确定它没有按预期返回?中间还有别的东西在编码吗?

我和邮递员一起测试了这个小片段

const express = require('express');
const app = express();

const encoded = encodeURI('http://example.com?query=ÅÍÎÏ˝ÓÔÒÚÆ☃');
const decoded = decodeURI(encoded);

app.get('/json', (req, res) => res.json({ encoded, decoded }));

app.listen(3000, () => console.log('Example app listening on port 3000!'));

感谢您编辑了我的问题,添加了更多信息。我需要转换的是html实体。更新的响应以匹配您的问题。TL;DR使用
unescape
+
replace
作为一种解决方法,但要在后端进行更新。