JQuery獲取並解析php返回的json格式數據的簡單示例


test.html示例如下:

<!DOCTYPE html>
<html>
    <head lang="en">
        <meta charset="UTF-8">
            <title>Test</title>
        </head>
        <body>
            <button id="btnTest">獲取JSON</button>
            <div>從服務器獲取的JSON: <span id="spanShowJson"></span></div>

            <script type="text/javascript" src="jquery-1.11.3.js"></script>
            <script>
            $(document).ready(function()
    	    {
               $('#btnTest').on('click', function()
               {
                   $.get("json.php", //設置url
               		function (data)
                	{
                   	 $('#spanShowJson').html(data.json);
                	}
                    );
               });
            });
            </script>
        </body>
    </html>

    
json.php示例如下:
    
<?php
   //設置http報頭
   header("Content-type: text/json; charset=utf-8");

   $jsonString = 'jsong string';
   $json = array('json'=>$jsonString);
   echo json_encode($json);
?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章