1. 程式人生 > >JQuery應用例項學習 —— 28 處理json返回值,點擊出現後臺返回資料

JQuery應用例項學習 —— 28 處理json返回值,點擊出現後臺返回資料

27.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery處理ajax的json返回值</title>
</head>
<body>
    <input type="button" value="看新聞">
    <ul>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</body>
<script src="jquery.js"></script>
<script>

// 點擊出現後臺返回資料
$('input').click(function(){
    $.get('27.php' , function(msg){ // msg回撥函式
        var i = 0;
        $('li').each(function(){ // 用面向函式的思想遍歷
            this.innerHTML = msg[i++];
        });
    } , 'json');
});

</script>
</html>

27.php

<?php 

$news = array(
'郭美美被抓',
'雲南地震',
'新疆反恐'
);
echo json_encode($news);

?>