简体   繁体   English

将数据从PHP传输到Javascript有哪些最佳实践?

[英]What are some best practices for transporting data from PHP to Javascript?

My projects are becoming more and more JS heavy. 我的项目变得越来越JS。 The issue is that most of my data is on the server side (PHP). 问题是我的大部分数据都在服务器端(PHP)。 What are some clean ways for transporting this data over to the client side (code samples for extra brownie points)? 有什么方法可以将这些数据传输到客户端(代码样本用于额外的布朗尼点)?

Some ways I am considering - which of these are best?: 我正在考虑的一些方法 - 哪些是最好的?:

1) Use php to "echo" directly to js variables. 1)使用php直接“回显”js变量。 2) Use php to write to HTML elements and then use js to retrieve from the dom. 2)使用php写入HTML元素,然后使用js从dom中检索。 3) Use ajaxcall 3)使用ajaxcall

Thoughts? 思考?

All of those methods are workable. 所有这些方法都是可行的。 As said, it really depends on the use case. 如上所述,它实际上取决于用例。

1) Use php to "echo" directly to js variables. 1)使用php直接“回显”js变量。

Which is a necessity if you need some data at "initialization time". 如果在“初始化时间”需要一些数据,这是必要的。 If your initial page display depends on some state variable, there's no way around this method. 如果您的初始页面显示取决于某个状态变量,则无法绕过此方法。

2) Use php to write to HTML elements and then use js to retrieve from the dom. 2)使用php写入HTML元素,然后使用js从dom中检索。

I consider this the lazy option. 我认为这是一个懒惰的选择。 It allows to update data without coding two different ways to retrieve it. 它允许更新数据而无需编写两种不同的方法来检索它。 Since jQuery provides .load("file.php div.data") you often can reuse existing output templates. 由于jQuery提供.load("file.php div.data")您通常可以重用现有的输出模板。

3) Use ajax calls 3)使用ajax调用

That's probably the best option for querying. 这可能是查询的最佳选择。 Especially if you need information from the database or poll some status this is the preferred way. 特别是如果您需要来自数据库的信息或轮询某些状态,这是首选方式。 Use JSON for returning data, but prefer GET/POST variables for client->server communication. 使用JSON返回数据,但更喜欢GET / POST变量用于客户端 - >服务器通信。

I prefer JSON with json_encode(); 我更喜欢JSON和json_encode(); in php using ajax call to retrive it. 在php中使用ajax调用来检索它。

4)使用JSON http://www.php.net/manual/en/function.json-encode.php - 返回值是JavaScript的关联数组

i personnally use a native PHP array and use json_encode and json_decode over it to get a clean Javascript-parsable array. 我个人使用本机PHP数组并使用json_encode和json_decode来获得一个干净的Javascript可解析数组。 You can have a look at the manual here: http://php.net/manual/en/function.json-encode.php 您可以在这里查看手册: http//php.net/manual/en/function.json-encode.php

The way I have found to perform the best is as follows. 我发现表现最佳的方式如下。

I make AJAX calls to a PHP page which performs some action, and returns data. 我对执行某些操作并返回数据的PHP页面进行AJAX调用。 Once that PHP has all the data it needs to return, I echo the data (as an array) in JSON format. 一旦PHP拥有它需要返回的所有数据,我就以JSON格式回显数据(作为数组)。

fetchUserInfo.php fetchUserInfo.php

die (
    json_encode(
        array(
            "username" => "Dutchie",
            "id" => "27"
        )
    )
);

Then, you can use javascript (although I suggest jQuery for AJAX browser compatibility) to fetch the data into a json object. 然后,您可以使用javascript(虽然我建议jQuery用于AJAX浏览器兼容性)将数据提取到json对象中。

JS JS

$.getJSON(
    'fetchUserInfo.php?',   
    function(jsonObject) { 
        alert(jsonObject.username); 
    }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM