2011-12-16 60 views
1

我想這個AJAX功能轉換爲jQuery的功能,但林不知道它將如何在jQuery的轉換AJAX功能,jQuery的

function ajax_posta() { 
// Create our XMLHttpRequest object 
    var hr = new XMLHttpRequest(); 
// Create some variables we need to send to our PHP file 
    var url = "javas.php"; 


    hr.open("POST", url, true); 
// Set content type header information for sending url encoded variables in the request 
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
// Access the onreadystatechange event for the XMLHttpRequest object 
    hr.onreadystatechange = function() { 
     if (hr.readyState == 4 && hr.status == 200) { 
      var return_data = hr.responseText; 
      document.getElementById('status').innerHTML = return_data; 
     } 
    } 
// Send the data to PHP now... and wait for response to update the status div 
    hr.send("num=" + (--num)); // Actually execute the request 
    document.getElementById('status').innerHTML = "<img src = 'loading.gif' height =  '30'  width = '30'>"; 

} 
$(document).ready(function() 
{ 
    $('.eventer.button').click(function() { 
     var self = this; 
     $.post('javas.php', function (data) { 
      $(self).parent('div').find('.status').html(data); 
     }) 
    }); 
} 
) 
; 
</script> 
+0

+1忍受了我最初的草率:) – 2011-12-16 20:42:13

回答

2

做你非常有

$.ajax('javas.php', { 
    success: function(response) { 
      $(".status").html(response); 
    }, 
    data: "num=" + (--num) 
}); 

如果這些是您需要發送給您的請求的唯一兩條數據,那麼您可以使用$.post,但這裏的優點是,如果您想指定更多選項,比如contentType,那麼您所要做的就是將其添加到現有的選項對象。

+0

請你可以解釋一下:d – foshoeiyyy 2011-12-16 19:54:43