A communication example between PHP and JS JSON

Source: Internet
Author: User

A brief introduction to JSON, JSON (web Effects object notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy to machine parse and generate. It is based on JavaScript programming language, standard ecma-262 a subset of 3rd Edition-december 1999. JSON uses a completely language-independent text format, but it also uses a family of C-language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:


A collection of name/value pairs (a collection of name/value pairs). In different languages, it is understood as objects (object), records (record), structure (struct), dictionaries (dictionary), hash tables (hash table), a list of keys (keyed list), or associative arrays (associative Array).
The ordered list of values (an ordered list of values). In most languages, it is understood as an array.


PHP File code

<?php
$res [' id '] = $_post[' id '];
$res [' name '] = "elar";
$res [' age '] = "21";
$response = "Hello this is response". $_post[' id '];
echo Json_encode ($res);
?>

JS Code

<script type= "Text/javascript" >
function Getjson () {
var xmlhttp;
try {
Firefox, Opera 8.0+, Safari
XMLHTTP = new XMLHttpRequest ();
}
catch (e) {
Internet Explorer
try {
XMLHTTP = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (e) {

try {
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e) {
Alert ("Your browser does not support ajax! ");
return false;
}
}
}

Xmlhttp.onreadystatechange = function () {
if (xmlhttp.readystate = = 4) {
//alert (xmlhttp.responsetext);
var str = xmlhttp.responsetext;
document.getElementById (' Show '). innerHTML +=str;
//alert (str);
var obj = eval (' + xmlhttp.responsetext + ') ');
//var obj = eval ({"id": "123", "Name": "Elar", "Age": "21"}));
Alert (obj.name);
}
}
var data = "id=123";
Xmlhttp.open ("Post", "testjson.php", true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("id=123");
}
</script>
<input type= button "onclick=" Getjson () "value=" Press me! "/>
<div id=" show "></DIV>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.