Scimyst
Subscribe
  • Home
  • Discoveries
    • All
    • 1001 Mysteries
    • Aliens & UFOs
    • Animal World
    • Archaeology
    • Biology
    • Doomsday
    • Famous Landmarks
    • History
    • Mars Exploration
    • Military Science
    • Oceanography
    • Science Q&A
    • Scientific Discoveries
    • Space Science
    • Why
    • World Figures
    • World Wonders
    thanh pho trang 650

    The Lost City of 500 Years Containing Flesh-Eating Parasites

    the ancient buddha statue for women is very beautiful but extremely vivid 119168

    Chilling Ancient Punishments for Women with Beautiful but Bloodied Names

    creating the worlds first plasma lamp not using a wire just throw it away 133669

    Creating the world’s most durable plasma lamp that doesn’t “take a break” after a few days of use

    which country suffers the most natural disasters in the world every year 136695

    Which country suffers the most natural disasters in the world, “experiencing” the most super typhoons each year?

    strange spc exoplanet

    10 Most Strange Things of the Universe

    thuan tay trai 650

    Left-Handed People and 16 “Shocking” Facts

    cham do ben khoe mieng 650

    The Secret of the Two Red Dots at the Corner of the Mouth of the Tang Dynasty Concubine

    tinh van con cua 650

    Revealing Unseen Images of the Crab Nebula through the James Webb Space Telescope

    cau hon o phap 650

    Banning Bathing, Forcing Tears, and Strange Wedding Customs Around the World

  • Events
    • All
    • Science Stories
    • Scientific Projects

    Tonight announces the Ig Nobel Prize winners

    frog

    Frog Wins Ig Nobel Prize 2005

    canhcutIg

    Life is Not Lacking in Quirks

    Nobel Prize in Medicine 2005: A Modern Fairy Tale

    10 Weird Inventions Awarded the Ig Nobel Prize 2005

    nobelKT

    Why Did They Win the Nobel Prize in Economics?

    giaydientu

    “Electronic Paper” Set to Appear on the Market

    Development of Science and Technology in the South Central Coast and Central Highlands Provinces

    a russian scientist loves vietnam 1659

    A Russian Scientist Who Loves Vietnam

  • Lifestyle
    • All
    • Environment
    • Medicine & Health
    tdnong

    Global warming may be the cause of major storms

    traxanh1

    Green tea has effects against dementia

    Discovery of Cancer-Preventing Genes in the Human Body

    Khachdulich Indonesia

    Avian influenza harms tourism more than terrorism

    nao

    Lying – A Disease of People with Brain Defects

    cafe

    Coffee May Prevent Cancer

    tebaocum

    Recreating the ‘Death’ Flu Virus of 1918

    Indo ban

    The whole world unites to fight avian influenza

    HIV aids

    The Mystery of AIDS Epidemic

  • Technology
    • All
    • AI - Artificial Intelligence
    • Computer Science
    • New Technology
    • Scientific Inventions
    top apps explore space solar system on pc 98000

    Top Applications for Exploring the Universe and Solar System on PC

    vai dien tu 650

    Soon there will be data-transmitting clothing

    sua loi khong vao dc coc coc 650

    Why can’t Cốc Cốc download videos to the computer?

    may tinh cu 1

    17 Things You Can Do with an Old Computer

    play free games super fun on android devices 54708

    Play Extremely Fun Free Games on Android Devices

    top 10 best weather apps on android 98127

    Top 10 Best Astronomy Apps on Android

    how to charge iphone properly 122474

    How to Charge an iPhone the Right Way

    do not scan qr code finger 122198

    Don’t Scan QR Codes Randomly

    sensor technology for measuring oxygen in blood 120461

    Light-Emitting Sensor Technology Can Measure Blood Oxygen

  • Cultural Heritage
    ai cap 650

    The Ruins of Memphis & the Pyramid Complex from Giza to Dahshur – Egypt

    khao co 650

    Prehistoric Archaeological Sites in the Dordogne Valley and Caves in the Vézère Valley – France

    baroque churches philippines 67188

    Baroque Churches – Philippines

    mont saint michel france 67265

    Mont Saint Michel Island – France

    acropolis bracelet in athens greece 67621

    Acropolis Fortress in Athens – Greece

    language mo tan thuy hoang china 67087

    Tomb of Qin Shi Huang – China

    historical city ayutthaya 67804

    Historical City of Ayutthaya

    mesa verde national park 67201

    Mesa Verde National Park – United States of America

    to the taj mahal india 67153

    Taj Mahal Temple – India

  • Entertainment
    • All
    • Photo Library
    • Videos

    Honesty

    Telephone

    Advanced Technology

    Terminal Illness

    Allergy

    Love Like Mars

    Super Definition

    Coi Sách

    Selling Candy

No Result
View All Result
Scimyst
  • Home
  • Discoveries
    • All
    • 1001 Mysteries
    • Aliens & UFOs
    • Animal World
    • Archaeology
    • Biology
    • Doomsday
    • Famous Landmarks
    • History
    • Mars Exploration
    • Military Science
    • Oceanography
    • Science Q&A
    • Scientific Discoveries
    • Space Science
    • Why
    • World Figures
    • World Wonders
    thanh pho trang 650

    The Lost City of 500 Years Containing Flesh-Eating Parasites

    the ancient buddha statue for women is very beautiful but extremely vivid 119168

    Chilling Ancient Punishments for Women with Beautiful but Bloodied Names

    creating the worlds first plasma lamp not using a wire just throw it away 133669

    Creating the world’s most durable plasma lamp that doesn’t “take a break” after a few days of use

    which country suffers the most natural disasters in the world every year 136695

    Which country suffers the most natural disasters in the world, “experiencing” the most super typhoons each year?

    strange spc exoplanet

    10 Most Strange Things of the Universe

    thuan tay trai 650

    Left-Handed People and 16 “Shocking” Facts

    cham do ben khoe mieng 650

    The Secret of the Two Red Dots at the Corner of the Mouth of the Tang Dynasty Concubine

    tinh van con cua 650

    Revealing Unseen Images of the Crab Nebula through the James Webb Space Telescope

    cau hon o phap 650

    Banning Bathing, Forcing Tears, and Strange Wedding Customs Around the World

  • Events
    • All
    • Science Stories
    • Scientific Projects

    Tonight announces the Ig Nobel Prize winners

    frog

    Frog Wins Ig Nobel Prize 2005

    canhcutIg

    Life is Not Lacking in Quirks

    Nobel Prize in Medicine 2005: A Modern Fairy Tale

    10 Weird Inventions Awarded the Ig Nobel Prize 2005

    nobelKT

    Why Did They Win the Nobel Prize in Economics?

    giaydientu

    “Electronic Paper” Set to Appear on the Market

    Development of Science and Technology in the South Central Coast and Central Highlands Provinces

    a russian scientist loves vietnam 1659

    A Russian Scientist Who Loves Vietnam

  • Lifestyle
    • All
    • Environment
    • Medicine & Health
    tdnong

    Global warming may be the cause of major storms

    traxanh1

    Green tea has effects against dementia

    Discovery of Cancer-Preventing Genes in the Human Body

    Khachdulich Indonesia

    Avian influenza harms tourism more than terrorism

    nao

    Lying – A Disease of People with Brain Defects

    cafe

    Coffee May Prevent Cancer

    tebaocum

    Recreating the ‘Death’ Flu Virus of 1918

    Indo ban

    The whole world unites to fight avian influenza

    HIV aids

    The Mystery of AIDS Epidemic

  • Technology
    • All
    • AI - Artificial Intelligence
    • Computer Science
    • New Technology
    • Scientific Inventions
    top apps explore space solar system on pc 98000

    Top Applications for Exploring the Universe and Solar System on PC

    vai dien tu 650

    Soon there will be data-transmitting clothing

    sua loi khong vao dc coc coc 650

    Why can’t Cốc Cốc download videos to the computer?

    may tinh cu 1

    17 Things You Can Do with an Old Computer

    play free games super fun on android devices 54708

    Play Extremely Fun Free Games on Android Devices

    top 10 best weather apps on android 98127

    Top 10 Best Astronomy Apps on Android

    how to charge iphone properly 122474

    How to Charge an iPhone the Right Way

    do not scan qr code finger 122198

    Don’t Scan QR Codes Randomly

    sensor technology for measuring oxygen in blood 120461

    Light-Emitting Sensor Technology Can Measure Blood Oxygen

  • Cultural Heritage
    ai cap 650

    The Ruins of Memphis & the Pyramid Complex from Giza to Dahshur – Egypt

    khao co 650

    Prehistoric Archaeological Sites in the Dordogne Valley and Caves in the Vézère Valley – France

    baroque churches philippines 67188

    Baroque Churches – Philippines

    mont saint michel france 67265

    Mont Saint Michel Island – France

    acropolis bracelet in athens greece 67621

    Acropolis Fortress in Athens – Greece

    language mo tan thuy hoang china 67087

    Tomb of Qin Shi Huang – China

    historical city ayutthaya 67804

    Historical City of Ayutthaya

    mesa verde national park 67201

    Mesa Verde National Park – United States of America

    to the taj mahal india 67153

    Taj Mahal Temple – India

  • Entertainment
    • All
    • Photo Library
    • Videos

    Honesty

    Telephone

    Advanced Technology

    Terminal Illness

    Allergy

    Love Like Mars

    Super Definition

    Coi Sách

    Selling Candy

No Result
View All Result
Scimyst
No Result
View All Result
Home Technology Computer Science

Ajax Programming Techniques

by Sci
December 30, 2005
in Computer Science
0
Share on FacebookShare on Twitter

AJAX is one of the most trendy terms in the information technology sector today and serves as the technical secret behind major applications like Google, Flickr, GMail, Google Suggest, and Google Maps. Although this term has only emerged in recent months, InfoWorld’s May 2005 issue stated that AJAX is creating a revolution in the web world.

WHAT IS AJAX?

JavaScript, the programming language that runs in browsers, has been familiar in the web world since Netscape invented it. The development of technology and rising user demands have forced developers to create a different technique that allows for more complex tasks. In February 2005, the term Ajax began circulating on the Internet as a new technique for web applications. The remarkable success and strange appeal of Gmail, Google Suggest, and Google Maps drew significant attention to Ajax.

Ajax stands for Asynchronous JavaScript and XML – a technique that combines two powerful features of JavaScript highly valued by developers:

• Sending requests to the server without reloading the page

• Parsing and working with XML

Ajax applications revolve around a feature called XMLHttpRequest. The engineers of the Mozilla project began supporting this feature in Mozilla 1.0 (and Netscape 7). Apple also implemented a similar feature starting from Safari 1.2.

Ajax is a combination of a series of technologies that have recently attracted the attention of the industry. These include:

• Web page presentation based on XHTML and CSS, standards from W3C, well supported by Firefox (Mozilla), Safari (Apple), Opera, Netscape 8.0 (built on Firefox);

• Dynamic and interactive representation using Document Object Model, a W3C standard;

• Data exchange and processing using XML and XSLT, W3C standards;

• Asynchronous data retrieval using XMLHttpRequest;

• Using JavaScript to link everything together. Here, JavaScript refers to ECMAScript, a standard from ECMA, not Microsoft’s JScript.

In traditional web applications, the client sends an HTTP Request to the web server, and the web server responds with information in the form of HTML and CSS. AJAX allows for the creation of an Ajax Engine that sits between this communication. In this case, the requests to send and receive responses are managed by the Ajax Engine. Instead of returning data as HTML and CSS directly to the browser, the web server can send back data in XML, and the Ajax Engine will receive, parse, and convert it to XHTML+CSS for the browser to display. This process occurs on the client side, significantly reducing the load on the server while providing users with immediate processing results without needing to reload the page. Additionally, the combination of web technologies like CSS and XHTML greatly enhances the presentation of web interfaces and significantly reduces the amount of data that needs to be loaded. These are practical benefits that Ajax offers.

AJAX PROGRAMMING

Handling HTTP Requests

To send an HTTP Request to the server using JavaScript, you need to create an object of the class that provides this functionality. In Internet Explorer, this class exists as an ActiveX object named XMLHTTP. This object has been available since IE 4.0.
var httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);

If MSXML is installed, you can also call:

var httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

In Mozilla, Firefox, Opera 8.0, Safari, and other browsers, this class is named XMLHttpRequest. The XMLHttpRequest object is not yet a W3C standard (it may be approved by W3C in the future). The XMLHttpRequest object is supported in IE 5.0+, Safari 1.2+, Mozilla 1.0+/Firefox, Opera 8.0+, and Netscape 7+.
var httpRequest = new XMLHttpRequest();

Due to these differences, to ensure your application runs on multiple browsers, you can do the following:

if (window.XMLHttpRequest) { // Mozilla, Safari, …

httpRequest = new XMLHttpRequest();

} else if (window.ActiveXObject) { // IE

httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);

}

Because ActiveX in IE is very dangerous for users, in many cases this feature is disabled by default, so you need to check the user’s browser before calling the XMLHTTP object. This check is performed through the value of window.ActiveXObject. For example:

if (window.ActiveXObject) {

xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”)

}

else { …}

Some versions of Mozilla browsers do not function correctly when receiving a response from the server without a header containing the XML mime-type. To fix this issue, you can use the method to redefine the headers sent by the server in cases where it is not text/xml.

httpRequest = new XMLHttpRequest();

httpRequest.overrideMimeType(text/xml);

Next, you need to specify what you want to do after receiving the response from the server. At this stage, you simply inform the HTTP request object which JavaScript function will handle the response. This is done by setting the onreadystatechange property of the object to the name of the JavaScript function:

httpRequest.onreadystatechange = nameOfTheFunction;

Note not to use parentheses () after the function name and do not pass parameters to that function. Additionally, instead of providing the function name, you can use the technique of defining a dynamic function:

httpRequest.onreadystatechange = function() {

// do the thing

};

After you have declared what will happen when receiving the response, you proceed to send the request. You need to call the open() and send() methods of the HTTP request class:

httpRequest.open(GET, http://www.example.org/some.file, true);

httpRequest.send(null);

The first parameter of the open() call is the HTTP Request method – GET, POST, HEAD, or any method you want to use that the server supports. Be sure to write it in uppercase according to the HTTP standard; otherwise, some browsers like Firefox may not process the request.

The second parameter is the URL of the page to which you are sending the request. Due to security settings, you cannot call pages on a third-party domain. Note that if you do not call the correct domain across all pages, you will receive a permission denied message when calling open().

The third parameter establishes whether the request is asynchronous or not. If TRUE, the execution of the JavaScript function will continue while the server’s response has not yet arrived. This is the “A” in AJAX.

The parameter sent to the send() method can be any data you want to send to the server if you are using the POST method to send the request. The data should be in the form of a query string:

name=value&anothername=othervalue&so=on

The XMLHttpRequest object has a set of common properties across all supporting environments. Below is a list of the main properties of this object.

Handling Server Responses

Note when sending a request, you provide the name of the JavaScript function designed to handle the response.

httpRequest.onreadystatechange = nameOfTheFunction;

Let’s see what this function should do. First, the function needs to check the status of the request. If the status is 4, it means that your application has received a complete response from the server, which is a good sign to continue processing it.

if (httpRequest.readyState == 4) {

// no issues occurred and you have received the response

} else {

// not ready

}

Next, you need to check the status code of the HTTP server response. All possible codes can be referenced on the W3C site. In this article, we are particularly concerned with the response 200 (OK).

if (httpRequest.status == 200) {

// response status returns a good signal!

} else {

// there was a problem receiving and processing the request,

// for example 404 (Not Found)

// or 500 (Internal Server Error)

}

After checking the status of the request and the HTTP response status code, how to process the data sent by the server is up to you. You have two options:

• httpRequest.responseText – returns as a text string

• httpRequest.responseXML – returns as an XMLDocument object which you can traverse using JavaScript DOM functions

Example with Text Response

We will build a simple example. The JavaScript code sends a request to an HTML page, test.html. This page contains the line “I’m a test”, and then we use alert() to output the content of that file.

var httpRequest = false; // Initially no request

function makeRequest(url) {

httpRequest = false;

if (window.XMLHttpRequest) { // Check for XMLHttpRequest support in Mozilla, Safari,…

httpRequest = new XMLHttpRequest();

if (httpRequest.overrideMimeType) {

httpRequest.overrideMimeType(text/xml);

}

} else if (window.ActiveXObject) { // In IE, check if ActiveX is disabled

try {

httpRequest = new ActiveXObject(“Msxml2.XMLHTTP”);

} catch (e) {

try {

httpRequest = new ActiveXObject(“Microsoft.XMLHTTP”);

} catch (e) {

// Your alternative handling

}

}

}

if (!httpRequest) {

alert(Giving up 🙁 Cannot create an XMLHTTP instance);

return false;

}

httpRequest.onreadystatechange = alertContents;

httpRequest.open(GET, url, true);

httpRequest.send(null);

}

function alertContents() {

if (httpRequest.readyState == 4) {

if (httpRequest.status == 200) {

alert(httpRequest.responseText);

} else {

alert(There was a problem with the request.);

}

}

}


style=”cursor: pointer; text-decoration: underline”

onclick=”makeRequest(test.html)”>

In this example, the user clicks on the “Make a request” link in the browser, which calls the makeRequest() function with one parameter – the name test.html of the HTML file in the same directory. The request is then executed.

Next, you define the alertContents() function to handle the onreadystatechange event. alertContents() checks if the response has been received and if it is OK, it will execute alert() to output the content of the test.html file.

Example with XML Response

The example we just reviewed illustrates how to use the reponseText property of the request object. The following example will demonstrate the responseXML property.

The first step is to prepare the XML page that will be used for the subsequent request. The following page is named test.xml:

I’m a test.

In the code snippet above, we just need to replace the request line with:

…

onclick=”makeRequest(test.xml)”>

…

Then, in the alertContents() function, we need to replace the line alert(httpRequest.responseText); with:

var xmldoc = httpRequest.responseXML;

var root_node = xmldoc.getElementsByTagName(“root”).item(0);

alert(root_node.firstChild.data);

responseXML provides us with an XMLDocument object, and we will use DOM methods to access some data available in the XML page.

The example source code can be downloaded from the TGVT website, and you can also run it directly at http://goldenkey.edu.vn/ajax.

onreadystatechange

Event handler for an event that triggers every time the state changes.

readyState

Status: 0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete

responseText

String data returned from the server

responseXML

DOM-compatible document object of the data returned from the server

status

Numeric status code returned by the server, such as 404 for “Not Found” or 200 for “OK”

statusText

String message accompanying the status code.

Pham Cong Dinh
Golden Key Language Center

——————————–
References
1. Effective Ajax Workshop Documentation, August 2005 (Jim Halberg and Rob Sanheim)
2. HTTP Status Codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
3. DOM:
http://www.w3.org/DOM/

Next Post
should remember mp3 format that quality does not decrease 2499

Compress MP3 format without losing quality

the hive does not create a culture suitable for patient 2500

Mr. Hwang did not create patient-compatible stem cells

10 notable science and technology events in vietnam 2005 2501

10 Notable Science and Technology Events of 2005

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

FEATURED POST

the mystery of the mummified woman misidentified as an egyptian pharaoh 129359

The Mystery of the Mummy of the Woman Mistaken for the Pharaoh of Egypt

space monsters in mythology first appearance 129358

Cosmic Monster ‘in Legend’ Appears for the First Time

the most dangerous coastline in the world haunting the dreams of mangrove forests 108247

The world’s most dangerous strait – The haunting toll measured in sailors’ lives

10 amazing natural phenomena that challenge science 60818

23 Mysterious Natural Phenomena That Challenge Science

TRENDING

no cave no peak excluding china leaves the world amazed when building a bridge like a dragon flying over the river 136956
Scientific Discoveries

No Tunnels, No Supports: China Stuns the World with a Bridge that Floats like Silk on the River

10 amazing natural phenomena that challenge science 60818
1001 Mysteries

23 Mysterious Natural Phenomena That Challenge Science

nguoi ai cap co dai 650
Scientific Discoveries

What Race Are the Egyptians Ultimately? Why Are They Different from Today’s Africans?

grand canyon mysterious area ancient egyptian ruins of the united states 133940
1001 Mysteries

Grand Canyon, the mysterious area containing ancient Egyptian relics of the United States

EDITOR PICK'S

What Race Are the Egyptians Ultimately? Why Are They Different from Today’s Africans?

No Tunnels, No Supports: China Stuns the World with a Bridge that Floats like Silk on the River

17 Causes of Throat Tightness and Belching

Oclantis: The Underwater City Built by Octopuses

23 Mysterious Natural Phenomena That Challenge Science

The best way to charge a newly purchased phone battery to avoid battery degradation

12 Most Dangerous Poisonous Mushrooms in the World

Newsletter

THE MOST IMPORTANT WORLD NEWS AND EVENTS OF THE DAY

Subscribe to our mailing list to receives daily updates direct to your inbox!

Category

  • 1001 Mysteries
  • AI – Artificial Intelligence
  • Aliens & UFOs
  • Animal World
  • Antibiotic Resistance
  • Archaeology
  • Bacteria & Insects
  • Biology
  • Biotechnology
  • Climate & Weather

Site Link

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

About Us

At Scimyst, we uncover the wonders of science, history, and culture. From ancient mysteries to modern innovations, our stories inspire curiosity and connect you to the fascinating world around us. Explore the unknown with us!

  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2025 Scimyst - Your gateway to the wonders of science and culture

No Result
View All Result
  • Home
  • Discoveries
    • 1001 Mysteries
    • Aliens & UFOs
    • Animal World
    • Archaeology
    • Biology
      • Bacteria & Insects
      • Biotechnology
      • Plants
    • Doomsday
    • Famous Landmarks
    • History
      • This Day in History
    • Military Science
    • Mars Exploration
    • Oceanography
    • Science Q&A
    • Science Quiz
    • Scientific Discoveries
    • Space Science
    • Why
    • World Figures
    • World Wonders
      • Cultural Heritage
      • Vietnamese Heritage
  • Technology
    • AI – Artificial Intelligence
    • Computer Science
    • New Technology
      • Self-Driving Cars
    • Scientific Inventions
  • Lifestyle
    • Cancer
    • Environment
      • Climate & Weather
      • Disasters
      • Latest Storm Updates
      • Nature
      • Solutions
    • Medicine & Health
      • Antibiotic Resistance
      • Common Illnesses
      • Dengue Fever
      • Family Medicine Cabinet
      • Food Safety
      • Health
      • Influenza Outbreak
    • Scientific Applications
  • Events
    • Science Events
    • Science Stories
    • Scientific Projects
  • Entertainment
    • Photo Library
    • Videos

© 2025 Scimyst - Your gateway to the wonders of science and culture