Monday, 3 April 2017

How to parse response [object object]

The request is being sent from onclick of href=url. When this url is hit, I am returning data from server to client as a query string.

res.redirect("/#blog_post_details/?dataUrl="+response[0].attributes);

And when I print using:

var posts = DataMixin.getParameterByName(dataUrl);
console.log('posts', posts); //I get [Object, Object]

I also tried with JSON.Stringify: console.log('posts', JSON.Stringify(posts)); which prints the same with quotes "[Object,Object]"

Function to return query string:

getParameterByName: function (name, url) {
    if (!url) {
      url = window.location.href;
    }
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

Note: I am using expressjs with nodejs



via kittu

No comments:

Post a Comment