dongyang4615 2015-07-01 10:30
浏览 49
已采纳

不同类型的ajax实现

i have just started working on ajax for my chat application which i am making in php. While studying ajax online I cam across 2 ways in on different sites where ajax had been implemented. What is the difference between these 2 implementations of ajax? Any help will be greatly appreciated :)

First Implementation-

    <script type"text/javascript">
        $(document).ready(function() {
        var dept = '<?php echo $deptId; ?>';
        var interval = setInterval(function() {
            $.ajax({
                url: 'scripts/php/Chat.php',
                data: {dept:dept},
                success: function(data) {
                $('#messages').html(data);
            }
        });
        }, 1000);
    });
    </script>

Second Implementation-

<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!
try{

  // Opera 8.0+, Firefox, Safari
  ajaxRequest = new XMLHttpRequest();
}catch (e){

  // Internet Explorer Browsers
  try{
     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }catch (e) {

     try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
     }catch (e){

        // Something went wrong
        alert("Your browser broke!");
        return false;
     }
  }
 }

// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){

  if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
  }
}

// Now get the value from user and pass it to
// server script.
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;
var queryString = "?age=" + age ;

queryString +=  "&wpm=" + wpm + "&sex=" + sex;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null); 
}

  • 写回答

2条回答 默认 最新

  • dsb0003795 2015-07-01 10:47
    关注

    Functionality-wise, it could be argued that there is no difference between them.

    That said, the first "difference" between them is that the first method is using JQuery, and thus to use it, you need to have the JQuery Javascript library included in your project or the page where you need the ajax functionality. The second method, however, uses "plain ole Javascript".

    Again, the first (JQuery) method, handles a lot of the "dirty" details for you, providing you with an ($.ajax) interface and requiring only that you pass in some parameters:

    url : The URL you wish to call

    data : The data (GET or POST) you wish to pass to the URL

    success : The callback function that should be executed when the ajax request successfully returns.

    In doing this, this method abstracts the internal implementation from you. For example, it handles for you the following:

    1. browser-sniffing/capabilities detection
    2. readyStateChange (event) checks

    and some other mundane details.

    Besides, also, using the second method, you can rest-assured that your code will mostly work in the majority of scenarios (if not all), if you "honour" the interface specification of the $.ajax call. Using the second method, however, you'll need to do a lot of testing and checks to ensure that your code works across all browser and platform types.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错