본문 바로가기

jquery ajax, $.get(), $.post() 사용방법

반응형

[ajax]
$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});


[get, post]
$.get("test.php");

$.get("test.php", { name: "John", time: "2pm" } );

$.get("test.cgi", { name: "John", time: "2pm" },
   function(data){
     alert("Data Loaded: " + data);
   });

$.get('ajax/test.html', function(data) {
  $('.result').html(data);
  alert('Load was performed.');
});

var jqxhr = $.get("example.php", function() {
    alert("success");
  })
  .success(function() { alert("second success"); })
  .error(function() { alert("error"); })
  .complete(function() { alert("complete"); });


Example: Gets the test.php page contents which has been returned in json format (<?php echo json_encode(array("name"=>"John","time"=>"2pm")); ?>).
$.get("test.php", { "func": "getNameAndTime" },
   function(data){
     alert(data.name); // John
     console.log(data.time); //  2pm
   }, "json");

반응형

댓글


Copyright ⓒ SmartWeb All rights reserved.