我有一个请求调用,它将调用包含var:
的javascript文件var url = 'http://' + _environment + '/services.js';
request(url, function (error, response, body) {
if (!error && response.statusCode === 200) {
_services = body;
}
console.log(_services);
});
这将记录如下内容:
var services = {
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
确切地说,这一行与我最终的结果相同:
var _services = var services = {
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
如何将位于某个网址的js文件中的var传递给我的代码?
答案 0 :(得分:0)
您可以使用exec(_services)
,但更好的办法是删除var
部分并返回常规JSON,然后解析JSON。
让它归还..
{
"production": {
"global": {
"id": "id",
"test2": "http://test.com",
"test": "/test/test.test",
"test1": "test"
}
}
}
然后做var services = JSON.parse(_services);
// This is with jQuery, you can convert to standard JS
$("body").append("<script>"+_services+"</script>");