A simple example of implementing static page local dynamic with Ajax calling interface

Source: Internet
Author: User
Tags json
File one: Front-end template file news.html.

Used to nest PHP statements after loading the database content to produce news.htmls static files.

 

File two: Articlelist.js file.

$.ajax ({//
	interface address
	URL: ' http://static.com/api/hot.php ',
	//Request method
	type: ' Get ',
	//Return data type
	DataType: ' JSON ',
	//request failed when processing
	error:function () {},
	//Request succeeded when processing method
	Success:function (Result) {
		if (Result.code = = 1) {
			//The data returned from the interface is assembled with HTML statement html
			= ';
			$.each (Result.data,function (key,value) {
				html + = ' <li><a href= ' # # ' >+value.title+</li> ';
			});
			//Even if a static news.htmls file is requested, the static new.htmls file, JQ also dynamically adds the data to the static news.htmls static file in
			$ ("#hot_html"). HTML (HTML);
		else{
			//todo
		}
	}
);

File three: interface file-news.php.

<?php 
	/*************** here ***************//
	* Results from the database query and assembled into an array $arrres*/
	/*********************** ///
	
    Call the Show method to encapsulate and return the interface data back
    to show (1, ', $arrRes);
	
    Function Show ($code, $message, $data) {
    	$resule =array (
    		' code ' = + $code,
    		' message ' + $message,
    		' Data ' = $data,				
    	);
    	Convert data into JSON data and output to make Ajax receive
    	echo Json_encode ($result);
    }
? >


Related Article

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.