javascript - 将 xmlhttp javascript 转换为 jquery

标签 javascript jquery ajax

我的问题是我的代码真的又长又复杂,因为它是普通的 xmlhttp,我想将它切换到 jQuery。我很确定它有简写函数?这是我当前的代码:

function getContent() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            replace_page = xmlhttp.responseText;
            doc.getElementById("content").innerHTML = replace_page;

            $("#content").children().hide();
            $("#content").children().fadeIn("slow");

            //do some other stuff
        }
        navigating = false;
    };
    xmlhttp.open("GET",site_location+"/pages/"+page+".php",true);
    xmlhttp.send();
}

我找到了这个example ,这工作得很好,但我也必须将其用于表单......

有没有简单的方法可以改变它?我知道它可能会涉及序列化功能,对吗?

最佳答案

$.ajax() ;

$.ajax({
    type: 'GET',
    url: site_location+"/pages/"+page+".php",
}).done(function(html){
    $('#content').html(html).children().hide().fadeIn("slow");
})

或者更好地使用 load()因为目标是将 html 内容加载到目标元素

$('#content').load(site_location+"/pages/"+page+".php", function(){
    $(this).children().hide().fadeIn("slow");
})

关于javascript - 将 xmlhttp javascript 转换为 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19149769/

相关文章:

javascript - 优化Google应用程序脚本以实现自动增量列

javascript - iframe 子级与多个 iframe 的父级通信

jquery + AJAX + 双重调用

javascript - 语法错误: Unexpected token o in JSON at position 1 over ajax request

jquery - 如何在jquery中设置类="active"

javascript - 提交表单后保留下拉菜单的 $_GET 值

javascript - 如何用 Jest 模拟模块实例的方法?

javascript - Livequery 不受支持的伪问题

多个 ajax 调用的 jQuery 回调

javascript - 全局覆盖 ajax beforeSend