jQuery.getJSON() trong jQuery

jQuery.getJSON() trong jQuery

jQuery.getJSON() trong jQuery

Learn more »


Định nghĩa jQuery.getJSON() trong jQuery

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.

Cú pháp Phương thức jQuery.getJSON(url,[data],[callback])

​ 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.

  • callback: − tham số tùy ý này biểu diễn một hàm để được thực thi bất cứ khi nào dữ liệu được tải thành công.

Ví dụ minh họa phương thức jQuery.getJSON() trong jQuery

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

Integrations
Users

Share Profile

Anyone at KeenThemes can view
Anyone with link can edit

Give Award

Anyone at KeenThemes can view
Anyone with link can edit

Report User

Let us know why you’re reporing this person
Don't worry, your report is completely anonymous; the person you're
reporting will not be informed that you've submitted it