如何获取JavaScript中的数组的Session值?

时间:2016-04-12 18:06:02

标签: javascript php arrays session

我在php页面中设置了一个会话,它存储了一个数组,如下所示:

firstpage.php

   $_SESSION["Counts"]=$some_array;
   echo print_r($_SESSION["Counts"]); 

输出:

Array ( [Finance] => Array ( [0] => 0 [1] => 3 [2] => 0 [3] => 0 [4] => 1 ) 
[Human resources] => Array ( [0] => 1 [1] => 5 [2] => 1 [3] => 0 [4] => 0 )
[Infrastructure] => Array ( [0] => 0 [1] => 3 [2] => 1 [3] => 0 [4] => 0 ) ) 1

在.js页面中检索会话数据 SecondJSpage.js

<script type="text/javascript">
var sessionValue= new Array();
var s= new Array();
var s1= new Array();

sessionValue = '<?php $_SESSION["Counts"]; ?>';
document.write(sessionValue);   //Does not output anything

for( s1 in sessionValue) {
for( s in s1) {

    document.write(s);     //Does not output anything
    document.write("<br />");
}}
</script>

仅检索数组。将显示一个简单的会话变量。如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

为此目的使用java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) at sun.security.ssl.SSLSocketImpl.<init>(Unknown Source) at sun.security.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source) at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:82) at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:127) at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707) at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387) at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323) at org.wso2.mobile.task.TaskImplementor.execute(TaskImplementor.java:53) at org.wso2.carbon.ntask.core.impl.TaskQuartzJobAdapter.execute(TaskQuartzJobAdapter.java:67) at org.quartz.core.JobRunShell.run(JobRunShell.java:213) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)`enter code here` at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) (Javascript可以处理JSON)。

查看手册:http://php.net/json_encode

答案 1 :(得分:0)

改变这一行......

sessionValue = <?php echo json_encode($_SESSION["Counts"]); ?>;

答案 2 :(得分:0)

你不能echo数组。

<?php echo $_SESSION["Counts"]; // this is an array ?>

你也不能在.js文件中执行PHP(尝试在SecondJSpage.js中解析PHP)