Ajax XMl http request

XMLHttpRequest

Once the XMLHttpRequest object has been created, it can be used to communicate directly with the server.  The first thing that should be done is to create a function that will be able to “catch” the information returned by the server.  This object contains a property called onreadystatechange, which stores the function that will process the server response. The following example code defines an empty function that will receive server data in addition to setting the onreadystatechange property.

 

Example: ajaxRequest.onreadystatechange = somefunction(){

// We still need to write some code here

}

 

The XMLHttpRequest object has a property called readyState. That stores the status of the server’s response. The response states are: processing, downloading or completed. The onreadystatechange property stores a function that will be executed each time the readyState changes.

For the example below, we are only interested in the “complete” readyState so that information can be obtained. Note that when the property is equal to a value of 4 the response is complete and data can be retrieved from the server.

Example: ajaxRequest.onreadystatechange = somefunction(){

if(ajaxRequest.readyState == 4){

}

}

 

When the response is complete, it is then possible to access the responseText property, which stores the response from the server.  JavaScript and HTML forms will allow you to set a text box to equal responseText.  Using the “someForm” form and a text box called “date”, the following example shows how to set the “date” text box to the server’s date.

Example: ajaxRequest.onreadystatechange = somefunction(){

if(ajaxRequest.readyState == 4){

document.someForm.date.value = ajaxRequest.responseText;

}

}

Once the onreadystatechange property has the necessary response-handling function, the request can be sent.  Achieving this requires the following two steps:

  1. Provide the URL for the server-side script used in the  Ajax application.
  2. Send the request with the send function

 

For the first step, we will use the example of a simple PHP script, called “serverDate.php”, that can be found in latter example.  For now, only the name is required.  The URL is set using the open method, which requires three arguments. The following example demonstrates the syntax for this method.

Example: ajaxRequest.onreadystatechange = somefunction(){

if(ajaxRequest.readyState == 4){

document.someForm.date.value = ajaxRequest.responseText;

}

}

 

ajaxRequest.open(“GET”, “serverDate.php”, true);

 

NOTE:  This example assumes that the PHP and HTML files are in the same directory.

 

The following example shows how to use the JavaScript to send a request to the server.

Example: ajaxRequest.onreadystatechange = somefunction(){

if(ajaxRequest.readyState == 4){

document.someForm.date.value = ajaxRequest.responseText;

}

}

ajaxRequest.open(“GET”, “serverDate.php”, true);

ajaxRequest.send(null);

 

Before the JavaScript is plugged into the HTML file, a way to run the AJAX function must be established. It would be optimal if the AJAX ran while the user is performing some action on the web page.  To accomplish this, the function will be set to run after the user enters their name and the onChange attribute will be used to make it so that the function is called whenever the user changes the user textbox.  An example of this is shown in the example below.

Example: <input type=’text’ onChange=”ajaxFunction();” name=’user’ /> <br />

 

The following example shows the JavaScript incorporated into the HTML file.

Example: <html>

<body>

 

<script language=”javascript” type=”text/javascript”>

<!–

//Support Code for Browser

function ajaxFunction(){

var ajaxRequest;  // This is necessary for AJAX

 

try{

// For Firefox, Opera, Safari

ajaxRequest = new XMLHttpRequest();

} catch (e){

// For Internet Explorer

try{

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

} catch (e) {

try{

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

} catch (e){

// Something is incorrect

alert(“Current browser does not work.”);

return false;

}

}

}

}

// Function to receive data from server

ajaxRequest.onreadystatechange = somefunction(){

if(ajaxRequest.readyState == 4){

document.someForm.date.value = ajaxRequest.responseText;

}

}

ajaxRequest.open(“GET”, “serverDate.php”, true);

ajaxRequest.send(null);

}

 

//–>

</script>

 

<form name=’someForm’>

Name: <input type=’text’ onChange=”ajaxFunction();” name=’user’ /> <br />

Date: <input type=’text’ name=’date’ />

</form>

</body>

</html>

This entry was posted in Ajax Tutorials and tagged , , , , , , , , , , , , . Bookmark the permalink.

33 Responses to Ajax XMl http request

  1. I relish, lead to I discovered just what I was taking a look for. You have ended my four day long hunt! God Bless you man. Have a great day. Bye

  2. Woh I like your blog posts, saved to fav! .

  3. health insurance should only be taken from reputable companies, you really don’t want to get it from fly-by-night companies,,

  4. Sup, this is fantastic guide. I absolutely enjoyed reading. However there are a lot of off topic comments. I seriously recommend you to delete or something like that. That’s only my estimation. All the best!

  5. Respect to article author , some good information .

  6. I really enjoy examining on this internet site , it has got great posts .

  7. I really enjoy examining on this internet site , it has got great posts .

  8. Found this on google and I’m happy I did. Well written article.

  9. 745908 435228Thanks for the post. I like your writing style – Im trying to start a weblog myself, I believe I may well read thru all your posts for some suggestions! Thanks once more. 795896

  10. 455991 375085How do I know if a WordPress theme supports a subscribe option? 853910

  11. satta matka says:

    473901 749393I like this post very considerably. I will undoubtedly be back. Hope that I can go by means of much more insightful posts then. Will likely be sharing your wisdom with all of my pals! 747343

  12. birkin bag says:

    I together with my friends ended up reading the best recommendations from your web page and then quickly got an awful suspicion I had not expressed respect to the website owner for those secrets. Most of the men appeared to be so happy to learn them and already have in fact been tapping into them. We appreciate you actually being very helpful and also for finding this sort of incredibly good subject areas millions of individuals are really desirous to discover. Our own sincere regret for not expressing gratitude to earlier.

  13. I am only writing to make you be aware of of the cool experience my cousin’s child enjoyed visiting your blog. She learned a lot of issues, which included what it is like to have an awesome helping spirit to get most people really easily thoroughly grasp a variety of very confusing topics. You really surpassed readers’ expectations. Thank you for offering such helpful, dependable, informative not to mention cool tips about this topic to Emily.

  14. I precisely needed to thank you so much again. I’m not certain the things that I would have gone through without those recommendations shared by you directly on my area. Previously it was a real horrifying setting in my circumstances, nevertheless encountering the specialised mode you handled the issue made me to cry over joy. Now i am grateful for this support and thus have high hopes you find out what a powerful job you were doing training the rest by way of a blog. I am certain you have never got to know any of us.

  15. bape hoodie says:

    I’m commenting to make you be aware of what a terrific discovery our daughter obtained reading through your web page. She noticed several things, including what it is like to have a marvelous helping nature to have other people effortlessly completely grasp a number of hard to do matters. You actually did more than her expected results. Many thanks for displaying the productive, trusted, informative as well as easy guidance on the topic to Lizeth.

  16. I would like to voice my admiration for your generosity for those people that have the need for assistance with your topic. Your very own dedication to getting the message around was certainly productive and have surely helped folks just like me to realize their aims. Your entire valuable help denotes a lot a person like me and somewhat more to my office workers. Thanks a ton; from all of us.

  17. I want to express appreciation to this writer just for bailing me out of this particular issue. Right after scouting through the world wide web and seeing concepts that were not pleasant, I assumed my entire life was well over. Living minus the approaches to the issues you have sorted out by means of the site is a crucial case, and the ones which could have in a negative way damaged my entire career if I hadn’t come across the blog. Your good know-how and kindness in taking care of all areas was priceless. I am not sure what I would’ve done if I had not discovered such a step like this. It’s possible to at this moment relish my future. Thanks a lot very much for this specialized and amazing help. I won’t be reluctant to endorse your web page to any person who needs counselling on this situation.

  18. I enjoy you because of your own work on this blog. My mum enjoys doing investigations and it’s really simple to grasp why. My partner and i notice all relating to the lively form you deliver efficient strategies on your website and as well improve participation from other people on that concern then my simple princess is without a doubt learning a lot of things. Take pleasure in the rest of the year. You’re carrying out a superb job.

  19. I as well as my guys have been looking at the best points from your website and so immediately I had a horrible suspicion I had not thanked the web site owner for those secrets. These young men are already as a consequence joyful to read all of them and already have honestly been taking pleasure in these things. Appreciate your genuinely simply helpful and then for making a choice on variety of cool ideas millions of individuals are really wanting to learn about. My very own honest regret for not expressing appreciation to earlier.

  20. yeezy boost says:

    My husband and i ended up being so satisfied when Jordan could do his investigation while using the ideas he grabbed from your own site. It’s not at all simplistic to just choose to be freely giving procedures that many some other people could have been selling. We really recognize we have got the writer to be grateful to because of that. All of the explanations you made, the straightforward site navigation, the relationships your site make it easier to instill – it’s mostly great, and it’s letting our son and us feel that the concept is brilliant, which is highly mandatory. Thank you for all the pieces!

  21. yeezy boost says:

    I precisely wished to appreciate you all over again. I am not sure what I would’ve done without the type of basics contributed by you regarding such a area. It absolutely was an absolute difficult crisis in my opinion, nevertheless being able to see the well-written technique you managed the issue forced me to jump with fulfillment. I’m grateful for the guidance as well as wish you are aware of a powerful job you were carrying out educating people today through the use of your webpage. Most probably you have never got to know all of us.

  22. I want to voice my respect for your generosity in support of those who should have help with this important theme. Your personal dedication to getting the solution all through was particularly useful and have usually allowed regular people like me to reach their endeavors. Your new important useful information implies a lot to me and substantially more to my office colleagues. Many thanks; from each one of us.

  23. I precisely had to say thanks yet again. I am not sure the things I might have used in the absence of the type of advice provided by you directly on such subject matter. It absolutely was a real alarming concern in my view, nevertheless observing your specialised manner you solved that forced me to weep over fulfillment. I am grateful for this help and even trust you are aware of a powerful job you happen to be accomplishing educating the mediocre ones through your webblog. Most likely you haven’t met all of us.

  24. I actually wanted to type a word in order to express gratitude to you for some of the fabulous points you are writing at this website. My incredibly long internet lookup has at the end been honored with beneficial information to exchange with my contacts. I would point out that many of us website visitors are truly fortunate to exist in a decent place with so many special individuals with beneficial tips. I feel rather grateful to have come across your entire webpages and look forward to many more fabulous times reading here. Thanks a lot once more for all the details.

  25. adidas nmd says:

    My wife and i got now thrilled that Raymond could deal with his reports out of the ideas he acquired while using the web site. It’s not at all simplistic to just possibly be giving for free secrets and techniques that a number of people could have been selling. We acknowledge we have the writer to give thanks to for that. The specific illustrations you’ve made, the simple blog menu, the relationships you aid to instill – it is mostly superb, and it’s leading our son in addition to us feel that the idea is brilliant, which is certainly very pressing. Thank you for everything!

  26. foamposites says:

    I as well as my guys have already been reviewing the excellent secrets and techniques on your web page and then suddenly I got a terrible suspicion I had not thanked the web site owner for them. Most of the boys are actually as a result thrilled to see them and now have seriously been using them. Thank you for getting really kind and for going for varieties of good issues millions of individuals are really eager to be aware of. Our honest regret for not expressing appreciation to you earlier.

  27. adidas nmd says:

    Thank you so much for giving everyone such a spectacular opportunity to check tips from this blog. It is often very pleasurable and stuffed with a good time for me personally and my office acquaintances to search your site at the least three times weekly to study the new guides you have got. Not to mention, I’m so certainly motivated with the wonderful creative concepts you give. Some 2 areas in this post are particularly the finest we have all had.

  28. Thank you so much for providing individuals with an extremely superb chance to read from this web site. It is often so awesome and as well , packed with amusement for me and my office co-workers to visit your web site at least 3 times every week to learn the new guidance you will have. Of course, we are certainly satisfied considering the surprising solutions served by you. Certain 2 ideas in this posting are in reality the simplest we have all ever had.

  29. After research a couple of of the blog posts on your web site now, and I truly like your way of blogging. I bookmarked it to my bookmark website record and shall be checking again soon. Pls take a look at my website as nicely and let me know what you think.

  30. yeezy shoes says:

    I as well as my friends happened to be reading through the nice tricks located on your site and so unexpectedly got a terrible feeling I never expressed respect to the blog owner for those strategies. All of the young men appeared to be totally stimulated to read through them and have in effect extremely been loving them. Many thanks for indeed being simply kind as well as for pick out such excellent information most people are really desirous to learn about. My very own honest regret for not saying thanks to you sooner.

  31. I enjoy you because of your whole labor on this blog. My daughter really likes engaging in investigation and it’s easy to understand why. I learn all regarding the lively means you create practical tips and hints by means of your blog and even attract participation from some others on the issue so our favorite girl has always been starting to learn a great deal. Take advantage of the rest of the year. You’re the one performing a good job.

  32. I’m just commenting to make you understand of the awesome encounter my child gained visiting your site. She picked up some details, with the inclusion of how it is like to have an excellent coaching mood to let other people smoothly grasp various complex things. You truly did more than her desires. Many thanks for distributing these useful, dependable, explanatory and in addition unique tips on that topic to Gloria.

  33. I definitely wanted to write down a small message to be able to express gratitude to you for all of the fabulous tips and tricks you are sharing at this site. My extended internet search has now been paid with extremely good insight to exchange with my relatives. I would tell you that we website visitors are truly blessed to be in a good site with many brilliant professionals with very beneficial points. I feel somewhat blessed to have used the webpages and look forward to plenty of more pleasurable moments reading here. Thank you once again for a lot of things.

Leave a Reply

Your email address will not be published.