如何在没有jQuery的情况下进行AJAX调用?如何使用JavaScript进行AJAX调用,而不使用jQuery?
4 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
使用“vanilla”JavaScript:
<script type="text/javascript">function loadXMLDoc() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4 if (xmlhttp.status == 200) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } else if (xmlhttp.status == 400) { alert('There was an error 400'); } else { alert('something else other than 200 was returned'); } } }; xmlhttp.open("GET", "ajax_info.txt", true); xmlhttp.send();}</script>
使用jQuery:
$.ajax({ url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); }});
添加回答
举报
0/150
提交
取消