javascript - 将 ajax 函数转换为 jquery

标签 javascript jquery html css ajax

我想将此 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/

相关文章:

javascript - 如何在 jquery slideToggle 中为 slideUp 和 slideDown 指定不同的持续时间?

php - 在 PHP 中,如何访问调用文件中定义的包含文件中的属性?

javascript - 如何提高Reactjs中大状态的性能?

javascript - 检测访问者是否来自 Google 搜索的最可靠方法?

javascript - 用于创建图片库的灯箱和传单

javascript - 如何获取 div 或任何子元素的标记名?

javascript - 使用 Css 或 Jquery 隐藏 Span 的第一个单词

javascript - JQuery 单击跨度中的文本?

javascript - 如何使用 Google Maps API v3 + 我自己的 map 图 block 显示标记

JQuery Datatable - 将单个单元格拆分为多个列以提高可读性?