无法使用javascript读取配置文件设置。

时间:2021-05-30 21:20:14

I need to read the config file setting in javascript. I wrote the below code in my aspx page. It's returning empty.

我需要在javascript中读取配置文件设置。我在aspx页面中编写了下面的代码。这是返回空。

<script type="text/javascript" language="javascript">

function GetFileLocationFromConfig(keyP) {

 var FileLocationL = '<%=ConfigurationManager.AppSettings[' + keyP+ '] %>';

 return FileLocationL;
            }
 </script>

2 个解决方案

#1


3  

You are confusing server side with client side.

你把服务器端和客户端搞混了。

The page is processed on the server and <% ... %> stuff is replaced with the result of computation serv side, then the generated page is sent to the client.

该页面在服务器上处理,<%…将%>替换为计算服务端的结果,然后将生成的页面发送给客户端。

Part of the page computed can be Javascript code, but you must understand and discern what computation is done in Javascript on the client and what computation is instead done on the server by ASP.

计算的页面部分可以是Javascript代码,但是您必须了解并了解在客户机上的Javascript中进行了什么计算,而在服务器上用ASP做了什么计算。

In you specific case a solution would be writing ASP code that generates a Javascript "dictionary" object for example producing something like

在您的特定情况下,一个解决方案是编写生成Javascript“dictionary”对象的ASP代码,例如生成类似的东西。

 var settings = {};
 settings["!key1"] = "value1";
 settings["!key2"] = "value2";
 settings["!key3"] = "value3";

then the lookup function can be implemented in Javascript as

然后,可以在Javascript中实现查找功能。

 function getSettingsValue(key) {
     return settings["!" + key];
 }

Be careful in knowing and understading exactly what you are sending to the client side by inspecting the generated page. For example sending passwords or other security related infos to the client would be a bad idea.

通过检查生成的页面,仔细地了解和隐藏您正在向客户端发送的内容。例如,向客户发送密码或其他安全相关的信息是一个坏主意。

#2


0  

You cannot pass javascript variable to PHP, ASP variables / methods.

您不能将javascript变量传递给PHP、ASP变量/方法。

You can do like this:

你可以这样做:

<?php

$arr = implode(",", ConfigurationManager.AppSettings);

?>

var s = '<?php echo $arr; ?>';
s = s.split(',');
var FileLocationL = s[ keyP ];

#1


3  

You are confusing server side with client side.

你把服务器端和客户端搞混了。

The page is processed on the server and <% ... %> stuff is replaced with the result of computation serv side, then the generated page is sent to the client.

该页面在服务器上处理,<%…将%>替换为计算服务端的结果,然后将生成的页面发送给客户端。

Part of the page computed can be Javascript code, but you must understand and discern what computation is done in Javascript on the client and what computation is instead done on the server by ASP.

计算的页面部分可以是Javascript代码,但是您必须了解并了解在客户机上的Javascript中进行了什么计算,而在服务器上用ASP做了什么计算。

In you specific case a solution would be writing ASP code that generates a Javascript "dictionary" object for example producing something like

在您的特定情况下,一个解决方案是编写生成Javascript“dictionary”对象的ASP代码,例如生成类似的东西。

 var settings = {};
 settings["!key1"] = "value1";
 settings["!key2"] = "value2";
 settings["!key3"] = "value3";

then the lookup function can be implemented in Javascript as

然后,可以在Javascript中实现查找功能。

 function getSettingsValue(key) {
     return settings["!" + key];
 }

Be careful in knowing and understading exactly what you are sending to the client side by inspecting the generated page. For example sending passwords or other security related infos to the client would be a bad idea.

通过检查生成的页面,仔细地了解和隐藏您正在向客户端发送的内容。例如,向客户发送密码或其他安全相关的信息是一个坏主意。

#2


0  

You cannot pass javascript variable to PHP, ASP variables / methods.

您不能将javascript变量传递给PHP、ASP变量/方法。

You can do like this:

你可以这样做:

<?php

$arr = implode(",", ConfigurationManager.AppSettings);

?>

var s = '<?php echo $arr; ?>';
s = s.split(',');
var FileLocationL = s[ keyP ];