Comprehensive analysis of JSON format and php transmission process in Ajax

  • 2021-08-21 19:49:58
  • OfStack

What are the small points to pay attention to in the transmission process between JSON format and php in Ajax?

First, look at 1 simple and general JSON and php data transmission code

HTML file:


<input type="button" value="Ajax" id="btn">
 <script>
  var btn = document.getElementById("btn");
  btn.onclick = function(){
    var xhr = getXhr();
    xhr.open("post"," Test .php");
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
     
    var user = '{"name":"zhangwuji","pwd":"123456"}';
    xhr.send("user="+user);
    xhr.onreadystatechange = function(){
      if(xhr.readyState==4&&xhr.status==200){
        var data = xhr.responseText;
        var json = eval("("+data+")");
        console.log(json);
      }
    }
  }
  function getXhr(){
    var xhr = null;
    if(window.XMLHttpRequest){
      xhr = new XMLHttpRequest();
    }else{
      xhr = new ActiveXObject("Microsoft.XMLHttp");
    }
    return xhr;
  }
 </script>

In the whole process first to obtain the AJAX object, and then use the POST request mode and PHP file connection, at this time the POST method is used to request data, so at this time also add a request file header

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); This is a fixed writing method, just write it down directly. < br > < br > The above is the test code, creating a string in json format and passing it into PHP using the SEND method:


var user = '{"name":"zhangwuji","pwd":"123456"}';

xhr.send("user="+user);<br><br> At this time, we should pay attention to the fact that when constructing JSON string transmission, the string inside user should use single citation outside and double citation inside, otherwise php doesn't think you are JSON and can't parse it correctly. < br > < br > < br > Look at the code in the PHP file at this time:


<?php
  //  Receive the request data sent by the client 
  $user = $_POST['user'];
  //  Is 1 A JSON Format string String 

  $json_user = json_decode($user,true);// Right json Format string is decoded and converted into PHP Variable format 

  // 2.  Use json_encode() Function 
  echo json_encode($json_user);// Right php Variable format is encoded and converted to JSON Format 
?>

json_decode and json_encode should all be able to see one point from the literal meaning. The function of decode here is

The string in json format is decoded and converted into PHP variable format

And encode is

Encode the php variable format, convert it into JSON format and transmit it back;

At this time, the work of PHP file is finished. Let's go back to HTML file and look at the code of onreadystatechange block that accepts data
:

var data = xhr.responseText;  Although the PHP file is transferred back in JSON format, we accept respenseText here, so we only receive a string in text format < br > At this time, we also use eval (); Function to convert it to JSON format


*  Use eval() Function for conversion 
          Use "()" Wrap it ,eval() Function to force it to be converted to JSON Format (javascript Code )
          Do not use "()" Wrap it ,eval() Function to identify it as 1 An empty code block 

Summarize


Related articles: