我想将此 ajax 函数转换为 jquery 函数,但我不确定在 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>
最佳答案
你已经差不多了
$.ajax('javas.php', {
success: function(response) {
$(".status").html(response);
},
data: "num=" + (--num)
});
如果这些是您需要发送给您的请求的仅有的两条数据,您可以只使用 $.post
,但这里的优点是如果您想要指定更多选项,与 contentType 一样,您所要做的就是将其添加到现有的选项对象中。
关于javascript - 将 ajax 函数转换为 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8539391/