jQuery getJSON()เมธอด

❮ วิธีการ jQuery AJAX

ตัวอย่าง

รับข้อมูล JSON โดยใช้คำขอ AJAX และส่งออกผลลัพธ์:

$("button").click(function(){
  $.getJSON("demo_ajax_json.js", function(result){
    $.each(result, function(i, field){
      $("div").append(field + " ");
    });
  });
});

ความหมายและการใช้งาน

เมธอด getJSON() ใช้สำหรับรับข้อมูล JSON โดยใช้คำขอ AJAX HTTP GET


ไวยากรณ์

$(selector).getJSON(url,data,success(data,status,xhr))

Parameter Description
url Required. Specifies the url to send the request to
data Optional. Specifies data to be sent to the server
success(data,status,xhr) Optional. Specifies the function to run if the request succeeds
Additional parameters:
  • data - contains the data returned from the server.
  • status - contains a string containing request status ("success", "notmodified", "error", "timeout", or "parsererror").
  • xhr - contains the XMLHttpRequest object

❮ วิธีการ jQuery AJAX