I'm working with Node.Js on server side, it generates a JSON, that I convert in an Object, and I need to access it from client side, in particular from a script in a HTML page.
EDIT: Server-side code
On the server side this is the code when the post request happens:
exports.plot = function(req, res){
var json;
request.post({
uri:"http://127.0.0.1:8080/api/v1/datapoints/query",
headers: {'Content-Type': 'application/json'},
body:req.body.txt1
},function (error, response, body) {
if (!error && response.statusCode == 200) {
var json = JSON.parse(body); //JSON converted in an object
res.render('query', json);
}
}
);
};
It was suggested me to use server side scripting (like the one used in ASP.NET) to access that object.
So I've used that syntax to get the values I want, however on the Chrome console I get this error "Uncaught SyntaxError: Unexpeted token (" .
Where I'm wrong?
Here's the code
<script>
function(){
var start = new Date();
var queryLength = <%= queries.length %>, // This gives me a number correctly
timestamps = [],
tagsString = [],
values = [],
dataPoints = [];
var metric = <%= queries[0].results[0].name %>; //Gives me correctly a string
//il for che mi scorre tutta la risposta
for (var j = 0; j < queryLength; j++)
{
}
}
</script>
the complete error is:
Uncaught SyntaxError: Unexpected token ( jquery-1.9.1.js:603
(anonymous function) jquery-1.9.1.js:603
jQuery.extend.globalEval jquery-1.9.1.js:604
jQuery.fn.extend.domManip jquery-1.9.1.js:6187
jQuery.fn.extend.append jquery-1.9.1.js:5949
(anonymous function) jquery-1.9.1.js:6074
jQuery.extend.access jquery-1.9.1.js:852
jQuery.fn.extend.html jquery-1.9.1.js:6038
(anonymous function) format-timestamp.js:36
fire jquery-1.9.1.js:1037
self.fireWith jquery-1.9.1.js:1148
done jquery-1.9.1.js:8074
callback jquery-1.9.1.js:8598
<%= queries.length %>and it is not surprising that there is a syntax error in your JS.forloop? Do you have more JavaScript code to show?<%= queries.length %>get translated into html?