开发者

can I inject data directly into session storage from server side selected data?

I'm wondering if it is possible to do a server side select on a mysql database and inject the result directly into HTML5 session storage without using ajax. The scenario is as follows:

Assume that OK on the server side script, thus:

    header( "Content-type: application/json" );

    $jsondata = array();

    while ($Row = mysql_fetch_array($params))
    {
    $jsondata[]= array('field1'=>$Row["field1"], 
                           'field2'=>$Row["field2"],
                           'field3'=>$Row["field3"],
                           'field4'=>$Row["field4"]);
    };

    echo json_encode(array("eeData" => $jsondata));

Normally, if this was ajax I'd wait for the response and I could parse the result and deal with it that way. But that method is initiated on the client side, and is in effect a second request to the server.

But, I was won开发者_开发知识库dering if this could be achieved in the absolute initial request for the page ie on the very first request, getting the data and somehow injecting it directly into the session storage at the same time as the page is first served.

Please make comments if the principle is not clear.


Exactly how is the webserver supposed to reach into the browser to store this data? Until the websockets api becomes more widespread, the ONLY way for a server to send data to a client is when the client initiates the request. You cannot just 'reach out' to a browser and stuff data into it.

If you want this during the initial page request, you can always embed the data into the page that's being sent

<script>
    var data_to_store = <?php echo json_encode($your_structure) ?>;
</script>

It'd make for a 'fat' page, but it'd send the data in the same request. Any other method would require a second connection back to the server.


I may be misunderstanding the question, but if you want to insert something into LocalStorage when the page first loads, just load it into a Javascript variable then insert it. For example:

...

var dataToInsert = JSON.parse("<?php echo(json_encode(arra("eeData" => $jsondata))?>");

Then just work with dataToInsert like any other JS variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜