记录日常点点滴滴,欢迎来到我的小站。

0%

jQuery Ajax技术 JsonP的利用(PHP)

PHP代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
header('Content-type: application/json');
//获取回调函数
$callback = $_REQUEST['callback'];

$conn = mysql_connect("192.168.0.139","root","root") or die ("connection fails!");
mysql_select_db("test",$conn);
mysql_query("set names 'UTF8'");//使用UTF8编码;

$sql = "SELECT DISTINCT eshop.shop_area, eshop.shop_name FROM en_shop AS eshop";
$result=mysql_query($sql);

//创建接收数组
$wp_options = array();

//整理数组
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
$wp_options[] = $row;
}

//json数据
$json = "[";
$db_data = "";
foreach ($wp_options as $data){
foreach($data as $key => $value){
if($key == 'shop_name'){
$db_data .= '"' . $key . '":'.'"'.$value.'"},';
}else{
$db_data .= '{"' . $key . '":'.'"'.$value.'",';
}
}
}

$json .= $db_data;
$json = rtrim($json,",");
$json .= "]";

echo $callback . '(' . $json . ')';

exit;
mysql_free_result($result);

?>

执行后得到以下状态

1
([{"shop_area":"Singapore","shop_name":"shop1"},{"shop_area":"Singapore","shop_name":"shop2"},{"shop_area":"Singapore","shop_name":"shop3"},{"shop_area":"Singapore","shop_name":"shop4"},{"shop_area":"Singapore","shop_name":"shop5"},{"shop_area":"Singapore","shop_name":"shop6"},{"shop_area":"Singapore","shop_name":"shop7"},{"shop_area":"Singapore","shop_name":"shop8"},{"shop_area":"Singapore","shop_name":"shop9"},{"shop_area":"Singapore","shop_name":"shop10"},{"shop_area":"Malaysia","shop_name":"shop1"},{"shop_area":"Malaysia","shop_name":"shop2"},{"shop_area":"Malaysia","shop_name":"shop3"},{"shop_area":"Malaysia","shop_name":"shop4"},{"shop_area":"Malaysia","shop_name":"shop5"},{"shop_area":"Malaysia","shop_name":"shop6"},{"shop_area":"Malaysia","shop_name":"shop7"},{"shop_area":"Malaysia","shop_name":"shop8"},{"shop_area":"Malaysia","shop_name":"shop9"},{"shop_area":"Indonesia","shop_name":"shop1"},{"shop_area":"Indonesia","shop_name":"shop2"},{"shop_area":"Indonesia","shop_name":"shop3"},{"shop_area":"Indonesia","shop_name":"shop4"},{"shop_area":"Indonesia","shop_name":"shop5"},{"shop_area":"Indonesia","shop_name":"shop6"},{"shop_area":"Indonesia","shop_name":"shop7"},{"shop_area":"Indonesia","shop_name":"shop8"},{"shop_area":"Indonesia","shop_name":"shop9"},{"shop_area":"Singapore","shop_name":"Shop223"},{"shop_area":"South Korea_re","shop_name":"Best Gate"},{"shop_area":"Oosaka","shop_name":"Naniwa denki"},{"shop_area":"Sapporo, Hokkaido","shop_name":"Aoki"},{"shop_area":"Area 01","shop_name":"SHOP Name 01"},{"shop_area":"Tanjong Pagar","shop_name":"PowerBuy"},{"shop_area":"kkkk","shop_name":"yyy"},{"shop_area":"aaa","shop_name":"aaa"},{"shop_area":"Akasaka Mitsuke, Tokyo","shop_name":"Mos Burger"}])

jQuery代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
$.ajax({
type:"get",
url:"http://192.168.0.139/education/shoplist",
dataType:"jsonp",
jsonp:"callback",
jsonpCallback:"success_jsonpCallback",
success: function(json){
$.each(json, function(i,v){
alert(v["shop_area"]);
});
},
error:function(){
alert('fail');
}
});
</script>

剩下的大家可以根据自己的需要进行修改。