Ajax Tutorial for Beginners: Part 1

Introduction

Hello everyone. This is my first article on The Code Project. I hope you will get some benefit from it. I have been learning Ajax for the last 5 months and now I want to share my knowledge with you. Please provide feedback on any mistakes which I have made in this article. Believe me guys, your harsh words would be received with love and treated with the top most priority.

 

There are many books and articles out there explaining the 5 Ws (Who, What, Where, When, Why) of Ajax, but I will explain to you in my own simple way. So what is Ajax? The term AJAX (Asynchronous JavaScript and XML) has been around for three years created by Jesse James Garrett in 2005. The technologies that make Ajax work, however, have been around for almost a decade. Ajax makes it possible to update a page without a refresh. Using Ajax, we can refresh a particular DOM object without refreshing the full page.

Background of Ajax

In Jesse Garrett’s original article that coined the term, it was AJAX. The “X” in AJAX really stands for XMLHttpRequest though, and not XML. Jesse later conceded that Ajax should be a word and not an acronym and updated his article to reflect his change in heart. So “Ajax” is the correct casing. As its name implies, Ajax relies primarily on two technologies to work: JavaScript and the XMLHttpRequest. Standardization of the browser DOM (Document Object Model) and DHTML also play an important part in Ajax’s success, but for the purposes of our discussion we won't examine these technologies in depth.

How Ajax Works

At the heart of Ajax is the ability to communicate with a Web server asynchronously without taking away the user’s ability to interact with the page. The XMLHttpRequest is what makes this possible. Ajax makes it possible to update a page without a refresh. By Ajax, we can refresh a particular DOM object without refreshing the full page. Let's see now what actually happens when a user submits a request:

  1. Web browser requests for the content of just the part of the page that it needs.
  2. Web server analyzes the received request and builds up an XML message which is then sent back to the Web browser.
  3. After the Web browser receives the XML message, it parses the message in order to update the content of that part of the page.

AJAX uses JavaScript language through HTTP protocol to send/receive XML messages asynchronously to/from Web server. Asynchronously means that data is not sent in a sequence.

Common Steps that AJAX Application Follows

The figure below describes the structure of HTML pages and a sequence of actions in Ajax Web application:

Let's See How Ajax Works in the Code

Let’s start to put these ideas together in some code examples.

  1. From the above code, our DOM object is (input type="text" id="lblTime") which we have to refresh without refreshing the whole page. This will be done when we press the event of a button, i.e. onclick.
  2. In this code, our handEvent() is GetTime() from the above Figure-2 if you take a look at it.

Let's see now how we create an XmlHttpRequest object and make Ajax work for us.

The basic implementation of the XMLHttpRequest in JavaScript looks like this:

1.Now from the above Figure-2 handEvent() i.e. GetTime() creates an XMLHttpRequest object inside it like this:

 

  1. If the browser does not support Ajax, it will return false.

  2. Next we open our connection to the server with our newly created XMLHttpRequest object. Here the Time.aspx page is the page from which we will get XML message that is stored on the Web server.

 
  1. You often hear the term “callback” replace the term “postback” when you work with Ajax. That’s because Ajax uses a “callback” function to catch the server’s response when it is done processing your Ajax request. We establish a reference to that callback function like this. Here StateChange is a function where we update or set a new value to our DOM object, i.e "lblTime".

    //Setup the callback function
    xmlHttpRequest.onreadystatechange = StateChange;

    Let's have a look at our callback function:

    function StateChange()
    {
        if(xmlHttpRequest.readyState == 4)
        {
    	document.getElementById('lblTime').value = xmlHttpRequest.responseText;
        }
    }

    onreadystatechange will fire multiple times during an Ajax request, so we must evaluate the XMLHttpRequest’s “readyState” property to determine when the server response is complete which is 4. Now if readyState is 4, we can update the DOM object with the response message we get from the Web server.

  2. As the request method we are sending is "GET" (remember it is case sensitive), there is no need to send any extra information to the server.

    //Send the Ajax request to the server with the GET data
    xmlHttpRequest.send(null);

    In Time.aspx.cs on Page_Load event, write a simple response like this which is our response message:

    Response.Write( DateTime.Now.Hour + ":" + DateTime.Now.Minute + 
    					":" + DateTime.Now.Second );

    That’s it. That’s Ajax. Really.

Key Points to be Remembered in Ajax

There are three key points in creating an Ajax application, which are also applicable to the above Tutorial:

  1. Use XMLHttpRequest object to send XML message to the Web server
  2. Create a service that runs on Web server to respond to request
  3. Parse XMLHttpRequest object, then update to DOM object of the HTML page on client-side
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AAAI 2020的教程“可解释人工智能”将重点介绍可解释人工智能的概念、方法和应用。可解释人工智能是指人工智能系统能够以一种可理解的方式解释其决策和行为的能力。该教程将涵盖可解释人工智能的基本原则和方法,包括规则推理、可视化技术、模型解释和对抗性机器学习等。 在教程中,我们将首先介绍可解释人工智能的背景和意义,解释为什么可解释性对于人工智能的发展至关重要。然后,我们将深入探讨可解释人工智能的基本概念和技术,例如局部解释和全局解释。我们还将介绍一些关键的可解释性方法,如LIME(局部诠释模型)和SHAP(SHapley Additive exPlanations),并解释它们的原理和应用场景。 此外,我们还将探讨可解释人工智能在各个领域的具体应用,包括医疗诊断、金融风险管理和智能驾驶等。我们将分享一些成功的案例和实践经验,探讨可解释人工智能在实际应用中的挑战和解决方案。最后,我们还将讨论未来可解释人工智能的发展趋势和挑战,展望可解释性在人工智能领域的重要性和前景。 通过参加该教程,学习者将能够全面了解可解释人工智能的概念、方法和应用,理解其在实际应用中的重要性,掌握一些关键的可解释性技术和工具,并对可解释人工智能的未来发展有一个清晰的认识。希望通过这次教程,能够为学习者提供一个全面而深入的可解释人工智能学习和交流平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值