Node js JSON stringify causing quot in output Can t parse with Jquery

0 votes

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

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

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

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

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

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

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:

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?

Any thoughts?

Jun 16 in JQuery by gaurav
• 17,760 points
31 views

1 answer to this question.

0 votes

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.jade is 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

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)

answered Jun 16 by rajatha
• 7,040 points

Related Questions In JQuery

0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello, Here is the syntax using jQuery $.get $.get(url, data, ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,740 points
25,651 views
0 votes
1 answer

How can I select an element with multiple classes in jQuery?

Hello @kartik, If you want to match only ...READ MORE

answered Sep 10, 2020 in JQuery by Niroj
• 82,740 points
1,423 views
0 votes
1 answer

How to pass parameters in GET requests with jQuery?

Hello @kartik, Use data option of ajax. You ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,740 points
2,731 views
0 votes
1 answer

How to find a parent with a known class in jQuery?

Hello @kartik, Assuming that this is .d, you can write $(this).closest('.a'); The closest method returns the ...READ MORE

answered Oct 5, 2020 in JQuery by Niroj
• 82,740 points
2,258 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10 in Java by Soham
• 8,740 points
2,224 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,009 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to shorten repeating code with html function and return tags in jquery

Firstly note that replace() only accepts two arguments, the ...READ MORE

answered Jun 15 in JQuery by rajatha
• 7,040 points
28 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP