I have a request call that will call a javascript file that contains a var:
var url = 'http://' + _environment + '/services.js';
request(url, function (error, response, body) {
if (!error && response.statusCode === 200) {
_services = body;
}
console.log(_services);
});
This will log something like the following:
var services = {
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
So to be exact, this line is equivalent to what I am ending up with:
var _services = var services = {
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
How to I pass a var from a js file located at some url to my code?
you could use exec(_services)
, but a better idea would be to just drop the var
part and return regular JSON, then parse the JSON.
Make it return this..
{
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
Then just do var services = JSON.parse(_services);
// This is with jQuery, you can convert to standard JS
$("body").append("<script>"+_services+"</script>");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments