Phương thức jQuery.getJSON(url,[data],[callback]) trong jQuery tải dữ liệu JSON bởi sử dụng một HTTP GET Request.
jQuery.getJSON() trong jQuery trả về đối tượng XMLHttpRequest.
Sau đây là cú pháp cho jQuery.getJSON(url,[data],[callback]) trong jQuery:
$.getJSON( url, [data], [callback] )
Tham số
Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức jQuery.getJSON(url,[data],[callback]) trong jQuery:
url − Một chuỗi chứa URL để request được gởi tới
data: − tham số tùy ý này biểu diễn các cặp key/value mà sẽ được gửi tới server.
Giả sử chúng ta có nội dung JSON sau trong result.json file:
{ "name": "Zara Ali", "age" : "67", "sex": "female" } Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức jQuery.getJSON(url,[data],[callback]) trong jQuery:
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#driver").click(function(event){ $.getJSON('../result.json', function(jd) { $('#stage').html('<p> Name: ' + jd.name + '</p>'); $('#stage').append('<p>Age : ' + jd.age+ '</p>'); $('#stage').append('<p> Sex: ' + jd.sex+ '</p>'); }); }); }); </script> </head> <body> <p>Click on the button to load result.html file:</p> <div id="stage" style="background-color:#cc0;"> STAGE </div> <input type="button" id="driver" value="Load Data" /> </body> </html> Xem thêm tổng hợp jQuery Ajax
Lượt xem : 258
This is excellent news!
Haven't seen the build yet, I'll look now.
Checking the build now