JSON advanced Article 2 Transfer JSON Data Using AJAX

Source: Internet
Author: User

The previous article "JSON advanced Article 1 using JSON in PHP and javascript" demonstrates how to use JSON data in PHP and javascript, this article describes how to use AJAX to obtain JSON data to dynamically generate titles and prompt statements. This technology is effective when a static page requests dynamic data from the website background. Because the website homepage has a large volume of traffic, the whole page must be statically processed, however, some data on this page must be updated in real time. In this case, AJAX can be used in static pages to request the JSON data generated in real time in the background. For more information about AJAX technology, see PHP advanced AJAX technology for MySql database access. Here we will detail how to use AJAX to transmit JSON data.
 
 
 
This example contains json2.php and json2.html. A button is displayed on json2.html. After you press the button, an AJAX request is sent to obtain the data returned by json2.php.
 
1. json2.php
 
[Php]
<? Php
// By MoreWindows (http://blog.csdn.net/MoreWindows)
$ Article_array = array (
"Count" => 3,
Array (
"Id" => "001 ",
"Title" => "accessing MySql database using PHP ",
"Link" => "http://www.bkjia.com/kf/201112/115227.html"
),
Array (
"Id" => "001 ",
"Title" => "accessing MySql database using PHP intermediate Smarty technology ",
"Link" => "http://www.bkjia.com/kf/201112/115229.html"
),
Array (
"Id" => "001 ",
"Title" => "PHP advanced AJAX technology for MySql database access ",
"Link" => "http://www.bkjia.com/kf/201112/115230.html"
),
);
$ Article_json = json_encode ($ article_array );
Echo $ article_json;
?>
2.Json2.html
 
[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> ajax request for json </title>
<Script type = "text/javascript" src = "../jquery-1.7.min.js"> </script>
<Script type = "text/javascript">
// Display prompt
Function OnMouseEnterDivInfo (thisObj, title)
{
$ ("# Article_link" ).css ("position", "absolute ");
$ ("# Article_link" ).css ("left", "20px ");
$ ("# Article_link" ).css ("top", $ (thisObj). offset (). top + $ (thisObj). height ());
$ ("# Article_link" ).html ("link address" + title );
$ ("# Article_link"). slideDown ("fast ");
Certificate (thisobj).css ("background-color", "red ");
}
// Hide the prompt
Function OnMouseLeaveDivInfo (thisObj)
{
$ ("# Article_link"). hide ();
Certificate (thisobj).css ("background-color", "yellow ");
}
// Jquery obtains JSON data through AJAX
$ (Document). ready (function (){
$ ("# GetDataBtn"). click (function (){
$. Post ("json2.php", {}, function (data ){
Var g_jsonstr = JSON. parse (data );
Var ilen = g_jsonstr ['Count'];
Var detailhtml = "";
For (var I = 0; I <ilen; I ++)
{
Var divhtml = '<div id = \ "div _' + I + '\" onmouseenter = \ "OnMouseEnterDivInfo (this, \ ''+ g_jsonstr [I] ['link'] + '\'); \" onmouseleave = \ "OnMouseLeaveDivInfo (this); \"> ';
Divhtml + = 'Divhtml + = '</div> ';
Detailhtml + = divhtml;
}
$ ("# Detail" detail .html (detailhtml); // generate a new title Area
$ ("# Detail"). slideDown ("slow ");
});
});
});
</Script>
<Style type = "text/css">
Div
{
Font-family: sans-serif;
}
</Style>
</Head>
<Body>
<Input type = "button" id = "GetDataBtn" value = "generate data"/>
<Div id = "detail">
</Div>
<P> <span id = "article_link" style = "display: none; z-index: 100"> </span> </p>
</Body>
</Html>
The running effect is as follows:
 
 
 
The next article "JSON advanced Article 3 apache multi-domain name and JSON cross-domain problem (JSONP)" will show the JSON cross-domain problem and provide solutions.
 
 
 
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7206390

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.