簡體   English   中英

在ajax中添加簡單的GET或POST?

[英]Adding a simple GET or POST in ajax?

我知道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.
}

入門的好地方: https : //developer.mozilla.org/en/AJAX/

您的代碼段僅適用於非IE瀏覽器。 不要將MS排除在外! 使用此代碼

var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

現在,您可以使用此對象執行一個或兩個請求。

這里有一個非常全面的教程: https : //developer.mozilla.org/en/AJAX/Getting_Started

該教程將比以往更好地解釋它。

我想提出一些建議,使您的生活更加輕松。 使用jQuery! 它使ajax調用(以及任何javascript)更加容易。 您只需很少的代碼即可執行復雜的操作。

http://api.jquery.com/jQuery.ajax/這是為了您的參考。

如果您真的很喜歡Javascript,並且想以簡單的方式學習AJAX,我建議您使用http://w3schools.com/ajax/ajax_intro.asp ,它非常基礎並且易於理解。 您甚至可以在那里嘗試到目前為止所學的知識。

而且,就考慮Ajax而言,jQuery很簡單。 基本上,它使您擺脫了檢查瀏覽器兼容性和其他問題的所有麻煩。 我建議您看看Elad在上面提到的內容。 “少寫多做”是Jquery的標簽。 試一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM