Node.js JSON.stringify() 导致“ 在输出中。无法用 Jquery 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11147468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:14:48  来源:igfitidea点击:

Node.js JSON.stringify() causing " in output. Can't parse with Jquery

jqueryjsonnode.jsexpresspug

提问by Jamis Charles

I am using Node.js (with Express.js) to pass a JSON data object from the server to the client view.

我正在使用 Node.js(与 Express.js)将 JSON 数据对象从服务器传递到客户端视图。

When I render the JSON object directly to the view I get the JSON object shown on the page as expected (this WORKS):

当我将 JSON 对象直接呈现给视图时,我会按预期获得页面上显示的 JSON 对象(此有效):

pageprovider.findAllTag( function(error, pages){
    res.send(pages);
})

And my output looks like this (much bigger, many nested obj)

我的输出看起来像这样(更大,很多嵌套的 obj)

{"green":{"title":"green","pagesContaining": ""}}

When I try to pass it to my Jade View like this:

当我尝试像这样将它传递给我的 Jade View 时:

pageprovider.findAllTag( function(error, tagsJSONObj){
        //res.send(pages);

    pageprovider.findAll( function(error, pages){
        res.render('search_tags.jade', { locals: {
            title: 'Search by Tags',
            'pages': pages,
            tagsJSON: JSON.stringify(tagsJSONObj) //pass the tags data as a JSON obj
            }
        });
    }) //pageprovider.findAll
}) //pageprovider.findAllTag


The problem
When I pass 'tagsJSON' to the view, the output includes the html entities:

问题
当我将 'tagsJSON' 传递给视图时,输出包括 html 实体:

var obj = jQuery.parseJSON( "{"name": 'value'}");

JQuery throws an error because it doesn't like '"'. How can I get Node to give me the proper quote, or get jQuery to accept this format?

JQuery 抛出一个错误,因为它不喜欢 '"'。我怎样才能让 Node 给我正确的报价,或者让 jQuery 接受这种格式?

Any thoughts?

有什么想法吗?

回答by Juan Mendes

It's because when you call

这是因为当你打电话

    res.render('search_tags.jade', { locals: {
        title: 'Search by Tags',
        'pages': pages,
        tagsJSON: JSON.stringify(tagsJSONObj) //pass the tags data as a JSON obj
        }
    });

search_tags.jadeis meant to output HTML, so it encodes your quotes. You should use a renderer that doesn't HTML escape, or at least change your view so that your params aren't HTML encoded

search_tags.jade旨在输出 HTML,因此它对您的引号进行编码。您应该使用不转义 HTML 的渲染器,或者至少更改您的视图,以便您的参数不是 HTML 编码的

If you don't want something in the output escaped, use !{tagsJSON}within the view. However, when outputting JSON, there's no need for a view. you can just take your object, call JSON.stringify. I don't use JADE so I'm not sure if there is a way to create view that can just call JSON.stringify(), but that's what I've done in JSP, velocity, ASP, PHP and Code Igniter (not using JSON.stringify, instead it uses a JSON tool for the given language)

如果您不希望输出中的某些内容被转义,请!{tagsJSON}在视图中使用。但是,在输出 JSON 时,不需要视图。你可以拿你的对象,打电话JSON.stringify。我不使用 JADE,所以我不确定是否有一种方法可以创建可以调用的视图JSON.stringify(),但这就是我在 JSP、velocity、ASP、PHP 和 Code Igniter 中所做的(不使用JSON.stringify,而是使用给定语言的 JSON 工具)

回答by Inshua

in ejs, its <%- tagsJSON %>

在 ejs 中,它的 <%- tagsJSON %>

          ^ <---- Note the "-"

回答by user1001447

Better solution when using Swig.js

使用 Swig.js 时更好的解决方案

{{ data|json|raw }}

{{ data|json|raw }}

回答by Zhang

Swig Templating Engine method:

Swig 模板引擎方法:

Since Swig hasn't been mentioned, I'll add my version in.

由于没有提到 Swig,我将添加我的版本。

I came across this problem today and spent a good few hours trying to get it to work so I could send data to Chart.js: http://www.chartjs.org/docs/

我今天遇到了这个问题,并花了好几个小时试图让它工作,以便我可以将数据发送到 Chart.js:http: //www.chartjs.org/docs/

In my case, I was using Geddy.js instead of Express.js. Like the original poster, I also got the html escaped JSON string problem.

就我而言,我使用 Geddy.js 而不是 Express.js。和原来的海报一样,我也遇到了html转义的JSON字符串问题。

I am using Swig templating engine.

我正在使用 Swig 模板引擎。

Thanks to Juan Mendes for mentioning html escaping, I found this discussion by the developer of Swig:

感谢 Juan Mendes 提到html escaping,我找到了 Swig 开发者的这个讨论:

https://github.com/jnordberg/wintersmith-swig/pull/1

https://github.com/jnordberg/wintersmith-swig/pull/1

Which led me to search for an option to disable auto escaping.

这导致我寻找禁用自动转义的选项。

I first tried:

我第一次尝试:

{{ data|raw }} // didn't work

as it was mentioned in the github page, but that didn't work so I went to documentation and found this:

正如在 github 页面中提到的那样,但这不起作用,所以我去了文档并找到了这个:

http://paularmstrong.github.io/swig/docs/tags/

http://paularmstrong.github.io/swig/docs/tags/

Gold! :D

金子!:D

So final solution:

所以最终解决方案

{% autoescape false %}
var data = {{ data }}
{% endautoescape %}