gpt4 book ai didi

javascript - 在ajax中添加简单的GET或POST?

转载 作者:行者123 更新时间:2023-11-28 16:32:35 25 4
gpt4 key购买 nike

我知道ajax中的GET和POST方法是什么,但我想知道如何将它们实现为简单的代码,以便我可以更好地理解它,这是我找到的一个简单的代码:

<html>
<head>
<title>XMLHttpRequest in Mozilla</title>
<script type="text/javascript">
function Start()
{
try
{
xmlhttp = new XMLHttpRequest();
document.getElementById("Content").innerHTML="<h1>Using XMLHttpRequest Object</h1>";
}
catch (e)
{
document.getElementById("Content").innerHTML="<h1>XMLHttp cannot be created!</h1>";
}
}
</script>
</head>
<body>
<a href="javascript:Start()">Start</a>
<div id="Content"></div>
</body>
</html>

最佳答案

您要做的唯一一件事就是确定您的浏览器是否支持 XMLHttpRequest (在浏览器中不支持,在其他任何方面都支持)。您实际上并没有调用服务器。

这是开始学习 ajax 和 javascript 的一个很好的链接:

http://www.hunlock.com/blogs/AJAX_for_n00bs

请务必检查所有网站,而不仅仅是那个帖子。

function ajaxRequest() {
var AJAX = null; // Initialize the AJAX variable.
if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
AJAX=new XMLHttpRequest(); // Yes -- initialize it.
} else { // No, try to initialize it IE style
AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
} // End setup Ajax.
if (AJAX==null) { // If we couldn't initialize Ajax...
alert("Your browser doesn't support AJAX."); // Sorry msg.
return false // Return false, couldn't set up ajax
}
var url='http://somedomain.com/getdata.php?doc=sometext.txt'; // This is the URL we will call.
AJAX.open("GET", url, true); // Open the url this object was set-up with.
AJAX.send(null); // Send the request.

AJAX.onreadystatechange = function() { // When the browser has the request info..
if (AJAX.readyState==4 || AJAX.readyState=="complete") { // see if the complete flag is set.
callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
}

// End Ajax readystate check.
} // End Event Handler.
}

关于javascript - 在ajax中添加简单的GET或POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5367131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com