Jquery AJAX方法查询快递物流

PHP文件:

<?php

$code = $_GET['code'];

if($code  ==  ''){

}


$result = '{"msg":"","status":"0","data":{"info":{"status":"1","com":"ems","state":"3","context":[{"time":"1450252800","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 \u5df2\u7b7e\u6536,\u4ed6\u4eba\u6536[\u9f99\u9526\u82d1\u6295\u9012\u7ec4]"},{"time":"1450172897","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 \u9884\u7ea62015.12.16\u518d\u6295[\u9f99\u9526\u82d1\u6295\u9012\u7ec4]"},{"time":"1450153979","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 [\u9f99\u9526\u82d1\u6295\u9012\u7ec410220812]\u6b63\u5728\u6295\u9012"},{"time":"1450088166","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 \u9884\u7ea62015.12.15\u518d\u6295[\u9f99\u9526\u82d1\u6295\u9012\u7ec4]"},{"time":"1450062684","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 [\u9f99\u9526\u82d1\u6295\u9012\u7ec410220812]\u6b63\u5728\u6295\u9012"},{"time":"1450000825","desc":"\u3010\u9f99\u9526\u82d1\u6295\u9012\u7ec4\u3011 \u5230\u8fbe[\u9f99\u9526\u82d1\u6295\u9012\u7ec410220812]"},{"time":"1449887960","desc":"\u3010\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec4\u3011 \u8f6c\u4ed6\u5c40\u5904\u7406,\u539f\u56e0:\u975e\u672c\u7ad9\u8bd5\u4ed6\u5c40[\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec4]"},{"time":"1449886219","desc":"\u3010\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec4\u3011 [\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec410221806]\u6b63\u5728\u6295\u9012"},{"time":"1449871677","desc":"\u3010\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec4\u3011 \u5230\u8fbe[\u5929\u901a\u897f\u82d1\u6295\u9012\u7ec410221806]"},{"time":"1449866125","desc":"\u3010\u5317\u4eac\u3011 \u79bb\u5f00[\u5317\u4eac10000000]\uff0c\u4e0b\u4e00\u7ad9\u662f[\u4eac\u5929\u901a\u897f],\u603b\u5305[5602],\u90ae\u8def[\u6c99\u6cb3-J1]"},{"time":"1449838500","desc":"\u3010\u5317\u4eac\u5e02\u3011 [\u5317\u4eac\u5e0210000000]\u5df2\u7ecf\u5c01\u53d1,\u603b\u5305[5602]"},{"time":"1449827400","desc":"\u3010\u4e9a\u8fd0\u6751\u90ae\u5c40\u5927\u5b97\u4e8c\u7ec4\u3011 \u79bb\u5f00[\u4e9a\u8fd0\u6751\u90ae\u5c40\u5927\u5b97\u4e8c\u7ec410010120]\uff0c\u4e0b\u4e00\u7ad9\u662f[\u5317\u4eac\u5e02],\u603b\u5305[5434],\u90ae\u8def[\u5546\u51fd-\u5c0f\u5305\u51fa\u53e3]"},{"time":"1449820658","desc":"\u3010\u4e9a\u8fd0\u6751\u90ae\u5c40\u5927\u5b97\u4e8c\u7ec4\u3011 [\u5317\u4eac\u5e02\u4e9a\u8fd0\u6751\u90ae\u5c40\u5927\u5b97\u4e8c\u7ec410010120]\u5df2\u7ecf\u6536\u5bc4"}],"_source_com":""},"com":"ems","company":{"url":"http:\/\/www.kuaidi100.com\/all\/ems.shtml?from=openv","fullname":"EMS","shortname":"EMS","icon":{"id":"2","smallurl":"https:\/\/ss2.baidu.com\/6ONYsjip0QIZ8tyhnq\/it\/u=1807529516,3291075151&fm=58","smallpos":"0,944","middleurl":"https:\/\/ss1.baidu.com\/6ONXsjip0QIZ8tyhnq\/it\/u=1835223070,3312272045&fm=58","middlepos":"0,828","normal":"https:\/\/ss1.baidu.com\/6ONXsjip0QIZ8tyhnq\/it\/u=295567570,1377797753&fm=58"},"website":{"title":"www.ems.com.cn","url":"http:\/\/www.ems.com.cn\/"},"tel":"11183","auxiliary":[{"title":"\u7f51\u70b9\u67e5\u8be2","url":"http:\/\/www.ems.com.cn\/serviceguide\/tong_da_fan_wei.html"},{"title":"\u7f51\u4e0a\u5bc4\u4ef6","url":"http:\/\/www.ems.com.cn\/serviceguide\/zifeichaxun\/zi_fei_biao_zhun.html"}]},"source":{"logo":"https:\/\/ss2.baidu.com\/6ONYsjip0QIZ8tyhnq\/it\/u=1429564979,1787167512&fm=58","title":"\u6570\u636e\u6765\u81ea\u5feb\u9012100","url":"http:\/\/www.kuaidi100.com\/","name":"\u5feb\u9012100"}}}';

if($code == '9971121346085'){
	echo $result;
}else{
	echo '{"msg":" 该单号暂无物流进展,请稍后再试,或检查公司和单号是否有误","status":"-2"}';
}

Jquery:

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>EMS</title>
	<style type="text/css">
		#ems{
			position: absolute;
			left: 50%;
			width: 400px;
			height: auto;
			margin-left: -200px;
			background: lightGreen;
			text-align: center;
			min-height: 50px;
			height: 500px;
			overflow-y: scroll;
			overflow-x: hidden;
		}
		#ems input{
			height: 25px;
			margin-top: 10px;
		}
		#list{
			background: lightBlue;
		}
		#list li{
			text-align: left;
		}
	</style>
	
</head>
<body>
<div id="ems">
	<div>
		<input type="text" id="code">
		<input type="button" value="查询" id="btn">
	</div>
	<div id="list">
	</div>
</div>
<script type="text/javascript" src = 'jquery-1.11.2.js'></script>
<script type="text/javascript">
	$(function(){

		$('#btn').click(function(){
			//alert($('#code').val());
			$.ajax({
				url:'ems.php?code='+$('#code').val(),
				//data:$('#code').text(),
				datatype:'json',
				type:'get',
				success:function(data){
						//console.log(data);
						var data = JSON.parse(data);
						if(data.status == 0){
							var list = data.data.info.context;
							var tag = '';
							for(var  i = 0; i < list.length; i++){
								var item = list[i];
								var t = new Date();
								 t.setTime(item.time+'000');
								var str = t.getFullYear()+'年'+(t.getMonth()+1)+'月'+t.	getDay()+'日'+(t.getHours()>12?' 下午':' 上午')+t.	getHours()%12+':'+t.getMinutes()+':'+t.getSeconds();
								tag += '<li><div>'+str+'</div><div>'+item.desc+'	</div></li>';
							}
							var ul = $('<ul></ul>');
							ul.html(tag);
							$('#list').append(ul);
						}
						else{
							var info = data.msg;
							$('#list').text(info);
						}
					},
				error:function(e){
					console.log(11);
				}
			});	
		});
	});
</script>
</body>
</html>

运行结果:

        过程中遇到了一个问题,就是能够请求到php文件,有返回的data,但是结果就是为空。后来发现原因是没有将返回的data转换成JSON格式,因为事先知道AJAX方法把datatype设置成json后会自动把数据转换为json格式,对于这个问题查询到一个解释:https://blog.csdn.net/haiqiao_2010/article/details/12653555

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页