Home > Article > Web Front-end > How front-end and back-end data should interact scientifically
This time I will bring you how to scientifically interact with front-end and back-end data. What are the precautions for front-end and back-end data interaction? Here are practical cases, let’s take a look.
HTML assignmentJS assignmentscript fills JSONAJAX gets JSONWebSocket real-time transmission of data Summary1. HTML assignment output to the value or data-name of Element<div data-value="<?php echo $user_avatar;?>"></div>Rendering result
<div data-avatar="https://avatars1.githubusercontent.com/u/3949015?v=3&s=40"></div>Use JS to get $('input').val();
$('div').data('avatar');Advantages: It does not occupy global variables and can be obtained freely by JS.
Usage suggestions: Suitable for
passing simple data, and also very suitable for binding relationships between multiple simple data and Element.
<li>nimojs <span data-userid="1" >删除</span></li> <li>nimo22 <span data-userid="2" >删除</span></li> <li>nimo33 <span data-userid="3" >删除</span></li> <li>nimo44 <span data-userid="4" >删除</span></li> <li>nimo55 <span data-userid="5" >删除</span></li></ul><script> $('span').on('click',function(){ $.post('/ajax/remove/',$(this).data('userid'),function(data){ // ... }) })</script>2. JS assignment
Fill data into the
JavaScript variable declaration of 3f1c4e4b6b16bbbd69b2ee476dc4f83a.
<script> var user_avatar = "<?php echo $user_avatar;?>"; // 渲染结果 // var user_avatar = "https://avatars1.githubusercontent.com/u/3949015?v=3&s=40";</script>Or use Smarty backend
Template engine:
<script> var user_avatar = "{$user_avatar}";</script>Advantages: Passing data is very convenient. The front end directly calls the user_avatar variable to use the data.
Disadvantages:
// The rendering result has a newline character var user_id = "https://avatars1.githubusercontent.com/u/3949015?v=3&s=40" ;// Uncaught SyntaxError: Unexpected token ILLEGAL
<script type="text/template" id="data">{"username":"nimojs","userid":1}</script><script> var data = JSON.parse($('#data').html()); //{username:"nimojs",userid:1}</script>Advantages: Data can be obtained after the page is loaded. It does not occupy global variables and can pass large amounts of data collections.
Disadvantages: When the amount of data is particularly large, the initial loading of the page will be slow. The slowdown is not only caused by the file size, but also because it takes time for the server to query the data and return the collection. You can use AJAX to obtain JSON to complete on-demand loading and load waiting.
Usage suggestions: Suitable for passing large data collections that need to be used when the DOM is loaded. For example: the front-end controls page rendering, and the back-end fills the JSON data source into 3f1c4e4b6b16bbbd69b2ee476dc4f83a and the front-end uses a JavaScript template engine to render the page. How to choose a Javascript template engine?
<span id="showdata">查看资料</span> <div style="display:none;" id="box"> <h2>用户信息</h2> <p id="info">![](loading.gif)</p> </div> $('#showdata').on('click',function(){ $('#box').show(); $.getJSON('/ajax/userdata/',function(oData){ // oData = {"username":"nimojs","userid":1} $('#info').html('用户名:' + oData.username + '<br>用户ID:' + oData.userid); }) })This is an example of getting user information through AJAX. The process is as follows: Only view information is displayed on the pageThe user clicks to view informationDisplays user information and loading picturesSends to the server to obtain user information AJAX requestThe server returns a JSON string, $.getJSON automatically converts the returned JSON string into an objectFill the content into 081157ee46b27f016eb1f52f4ee681ceAdvantages: It does not occupy global variables and DOM nodes, and you can freely control the trigger conditions for obtaining data (when the page is loaded, when the user clicks to view information, or when the user clicks a button). When starting to obtain data, you can use the loading image placeholder to prompt the user that the data is being read. Prevents slow page loading caused by loading all the data on the page. Disadvantages: Additional HTTP requests will be generated. It cannot be obtained immediately after the DOM is loaded. It needs to send a request and receive a response. Usage suggestions: Suitable for loading non-main information, setting trigger conditions (when the user clicks to view data), and providing friendly data reading waiting prompts. 5. WebSocket transmits data in real time. If AJAX requests and responses are compared to sending text messages to the server and waiting for the server to reply to text messages, WebSocket is like making a phone call to the server.
I won’t introduce much about WebSocket here, but here are the reference materials:
WebSocket**Building real-time Web applications using HTML5 WebSocket**
Ajax vs WebSocket**6. Summary There is every situation There is no absolutely right way to be useful in situations. Flexibly choose the data acquisition method according to the actual situation. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Related reading:
Detailed explanation of the box model of html
The above is the detailed content of How front-end and back-end data should interact scientifically. For more information, please follow other related articles on the PHP Chinese website!