The full name of JSON is Javascript Object Notation (javascript object notation), which is based on javascript object literals. This article mainly introduces the relevant information about using native ajax to process json strings. Friends in need can refer to
AJAX
##AJAX = Asynchronous JavaScript and XML. AJAX is not a new programming language, but a new way of using existing standards. AJAX is the art of exchanging data with a server and updating parts of a web page without reloading the entire page.What is json?
The full name of JSON is Javascript Object Notation (javascript object notation), which is based on javascript object literals. If you look at it with your eyes, the data in JSON is stored in curly brackets. -- {}, if you further analyze the purpose, JSON is a data exchange format. JSON is internally arranged in the format of name:value, such as json1 below, which is a json object.var json1={"name":"李明","age":21,"sex":"boy"}
What is a json string?
As its literal meaning, add double quotes (or single quotes) to both sides of json. In order to avoid conflict with the internal double quotes, we put single quotes outside, which becomes a json character. string, such as the following json2var json2='{"name":"李明","age":21,"sex":"boy"}'2. In our data supply page, output our json, we still call the data supply page tigong.php The code is as follows:
<?php header("content-type:text/html;charset=utf-8"); echo '{"name":"黎明","age":"12","sex":"男"}'; ?>3. Receive data on our front-end page, use the eval method to parse the json string into a json object, and use a for loop to traverse it. We call this page testJsonEvel.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script type="text/javascript"> window.onload=function(){ var p1=document.getElementById("p1"); var bt1=document.getElementById("bt1"); bt1.onclick=function(){ //创建ajax对象,写兼容 if(window.XMLHttpRequest){ var xmlHttp=new XMLHttpRequest(); }else{ var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }; //获取要发送的数据,我们这个例子没有 //设置发送数据的地址和方法 xmlHttp.open("POST","tigongjson.php"); //设置我们的请求头信息 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //绑定onreadystatechange事件 xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ var data=xmlHttp.responseText; //json字符串转换成为json对象 data=eval("("+data+")"); var str=""; str+="姓名:"+data.name+"<br>"; str+="年龄:"+data.age+"<br>"; str+="性别:"+data.sex p1.innerHTML=str; }; }; //发送数据 xmlHttp.send(); }; }; </script> <style type="text/css"> #p1{ width:200px; height:200px; background:#f00; color:#fff; } img{ width:200px; } </style> <title>Document</title> </head> <body> <p id="p1"> <img src="/static/imghwm/default1.png" data-src="../php2/images/1.gif" class="lazy" alt="How to use native ajax to process json strings" > </p> <br> <button id="bt1">点击获取json数据</button> </body> </html>evel does not have compatibility issues, but there will be security holes. The effect is as shown:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script type="text/javascript"> window.onload=function(){ var p1=document.getElementById("p1"); var bt1=document.getElementById("bt1"); bt1.onclick=function(){ //创建ajax对象,写兼容 if(window.XMLHttpRequest){ var xmlHttp=new XMLHttpRequest(); }else{ var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }; //获取要发送的数据,我们这个例子没有 //设置发送数据的地址和方法 xmlHttp.open("POST","tigongjson.php"); //设置我们的请求头信息 xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //绑定onreadystatechange事件 xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==4 && xmlHttp.status==200){ var data=xmlHttp.responseText; //json字符串转换成为json对象 data=JSON.parse(data); var str=""; str+="姓名:"+data.name+"<br>"; str+="年龄:"+data.age+"<br>"; str+="性别:"+data.sex p1.innerHTML=str; }; }; //发送数据 xmlHttp.send(); }; }; </script> <style type="text/css"> #p1{ width:200px; height:200px; background:#f00; color:#fff; } img{ width:200px; } </style> <title>Document</title> </head> <body> <p id="p1"> <img src="/static/imghwm/default1.png" data-src="../php2/images/1.gif" class="lazy" alt="How to use native ajax to process json strings" > </p> <br> <button id="bt1">点击获取json数据</button> </body> </html>JSON.parse does not have security vulnerabilities, but it will have compatibility and is not supported by IE8 and below. The effect is as shown
Talk about the views and understanding of Ajax form submission
An example of writing a map through the database and ajax method Code
Ajax gets data through city name
The above is the detailed content of How to use native ajax to process json strings. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools
