Home  >  Article  >  Backend Development  >  How JavaScript handles php returning data in json format_PHP Tutorial

How JavaScript handles php returning data in json format_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:01908browse

How JavaScript handles php returning data in json format, let’s illustrate with an example below!

Suppose php returns the following array:

$arr = array(
	array(
		'name'=>'qianyuqianxun',
		'nick'=>'千与千寻',
		'contact'=>array(
			'website'=>'http://www.phpernote.com'
		)
	),
	array(
		'name'=>'qisha',
		'nick'=>'七煞',
		'email'=>'yhm.1234@163.com',
		'contact'=>array(
			'website'=>'http://www.baidu.com'
		)
	)
);
print_r(json_encode($arr));
exit;

Then the client JS can call the data returned above as follows:

$(document).ready(function(){
	var url='http://www.phpernote.com/json.php'
	$('#submitBtn').click(function(){
		$.post(url,'',function(msg){
			var myObject=eval('('+msg+')');//msg为返回的类型为字符串,转化为json对象
			var str='';
			var len=myObject.length;
			for(i=0;i<len;i++){
				str+='<tr><td>'+myObject[i].name+'</td><td>'+myObject[i].nick+'</td><td>'+myObject[i].contact.website+'</td></tr>';
			}
			$('#feedbackTable').html(str);
		})
	})
})

Articles you may be interested in

  • How jquery processes json data
  • PHP number formatting, adding commas to every three digits of the number
  • mysql Queue realizes efficient concurrent reading of data
  • jquery+html+php realizes Ajax refresh-free file upload
  • Use MySQL database index appropriately to make the database run efficiently
  • Use separately in php Summary of curl's post method to submit data and get method to obtain web page data
  • PHP method to obtain each month within a certain period of time and return an array composed of these months
  • PHP connects to the access database Two methods

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764170.htmlTechArticleHow JavaScript handles php returning data in json format, let’s illustrate with an example below! Assume that php returns the following array: $arr = array(array('name'='qianyuqianxun','n...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn