Javascript 在ajax中添加简单的GET或POST?
我知道ajax中的GET和POST方法是什么,但我想知道如何将它们实现为一个简单的代码,以便更好地理解它,下面是我发现的一个简单代码:Javascript 在ajax中添加简单的GET或POST?,javascript,ajax,Javascript,Ajax,我知道ajax中的GET和POST方法是什么,但我想知道如何将它们实现为一个简单的代码,以便更好地理解它,下面是我发现的一个简单代码: <html> <head> <title>XMLHttpRequest in Mozilla</title> <script type="text/javascript"> function Start() { try { xmlhttp = new XMLHttpRequest(); documen
<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>
Mozilla中的XMLHttpRequest
函数Start()
{
尝试
{
xmlhttp=新的XMLHttpRequest();
document.getElementById(“Content”).innerHTML=“使用XMLHttpRequest对象”;
}
捕获(e)
{
document.getElementById(“内容”).innerHTML=“无法创建XMLHttp!”;
}
}
我想提出一些建议,让你的生活更轻松。使用jQuery!它使ajax调用(以及任何javascript)变得更加容易。您可以用很少的代码完成复杂的操作
这是供您参考的。我想提出一些建议,让您的生活更轻松。使用jQuery!它使ajax调用(以及任何javascript)变得更加容易。您可以用很少的代码完成复杂的操作
这供您参考。您唯一要做的就是确定您的浏览器是否支持XMLHttpRequest(在资源管理器中不支持,在其他任何方面都支持)。您实际上并不是在调用服务器 这是一个开始学习ajax和javascript的好链接: 确保你检查了所有的网站,而不仅仅是那篇文章
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.
}
您要实现的唯一一件事就是确定浏览器是否支持XMLHttpRequest(在资源管理器中为否,在其他任何方面为是)。您实际上并不是在调用服务器 这是一个开始学习ajax和javascript的好链接: 确保你检查了所有的网站,而不仅仅是那篇文章
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.
}
一个开始的好地方: 您的代码片段只能在非IE浏览器中工作。不要把MS排除在聚会之外!使用此代码
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
现在可以使用此对象执行一两个请求
这里有一个非常全面的教程:
那篇教程将比我能解释得更好 一个开始的好地方: 您的代码片段只能在非IE浏览器中工作。不要把MS排除在聚会之外!使用此代码
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
现在可以使用此对象执行一两个请求
这里有一个非常全面的教程:
那篇教程将比我能解释得更好 如果你真的很喜欢Javascript,并且想用简单的方式学习AJAX,我建议你使用简单易懂的方法。你甚至可以在那里尝试你迄今所学的东西
此外,就Ajax而言,Jquery非常简单。它基本上让你从检查浏览器兼容性和其他东西的混乱中解脱出来。我建议你看看Elad在上面提到的内容。“少写多做”是Jquery的标记。试一试。如果你真的很喜欢Javascript,并且想用简单的方式学习AJAX,我建议你使用非常简单易懂的方法。你甚至可以在那里尝试你迄今所学的东西
此外,就Ajax而言,Jquery非常简单。它基本上让你从检查浏览器兼容性和其他东西的混乱中解脱出来。我建议你看看Elad在上面提到的内容。“少写多做”是Jquery的标记。试一试。hehe,我看过jquery,对此我很困惑:我发现普通javascript对于简单调用非常有用。我并不是说jQuery对于其他任何东西都不是一个很好的库!我只是想,如果你想学习一项新技能,你不妨从中获得一些额外的借鉴:)呵呵,我看了jquery,对此我很困惑:我发现普通javascript对于简单调用非常有用。我并不是说jQuery对于其他任何东西都不是一个很好的库!我只是认为,如果你想学习一项新技能,你最好从中获得一些额外的借鉴:)我非常了解javascript,这对ajax是新的,那么链接应该就是你所需要的了。在答案中添加来自该文档的示例…我非常了解javascript,对于ajax xDWell来说这是一个新的功能,那么该链接应该就是您所需要的全部了。在答案中添加来自该文档的示例。。。