473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XMLHttpRequest question

I have an online store that a customer needs customized by having notes for
each product. I have added a javascript function that sends a session id,
unique product id and some user-defined notes to a script using
XMLHttpRequest. The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data back
from the database and display it. I don't need to display the notes in
order to get them to send, I just want to customer to see their notes so
they don't think they are lost.

So, in my checkout page I have a loop that created divs like the following
<div id="notes<number>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload function
call.

This onload function call looks something like this:
onload="getNotes(<number>,<session_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script and
using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatechange" function
gets called but only the code outside of the check for the "readyState" and
"status" is executed.
The stuff inside of
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatechange
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.

Nov 3 '05 #1
5 1610
Sean,

You are, indeed, checking the readyState before the server has a chance to
reply. You'd probably be better off using xmlhttp.readystatechange to watch
for that reply. Here's some code that should help:

xmlhttp.onreadystatechange = function() {
if ( xmlhttp.readyState == 4 ) {
if ( xmlhttp.status == 200 ) {
doSomeMagic();
} else {
didntWorkOut();
}
}
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
}
"Sean Berry" <se**@buildingonline.com> wrote in message
news:1Muaf.4771$UF4.2202@fed1read02...
I have an online store that a customer needs customized by having notes for
each product. I have added a javascript function that sends a session id,
unique product id and some user-defined notes to a script using
XMLHttpRequest. The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data
back from the database and display it. I don't need to display the notes
in order to get them to send, I just want to customer to see their notes
so they don't think they are lost.

So, in my checkout page I have a loop that created divs like the following
<div id="notes<number>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload
function call.

This onload function call looks something like this:
onload="getNotes(<number>,<session_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script and
using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatechange" function
gets called but only the code outside of the check for the "readyState"
and "status" is executed.
The stuff inside of
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatechange
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.

Nov 3 '05 #2
Thanks Mike.

I actually figured out the first of my problems. I was doing the following
xmlhttp.onreadystatechange = myfunction(param1, param2, param3);

I changed that to
xmlhttp.onreadystatechange = function() {myfunction(param1, param2,
param3)};

Now it works as I expected in my first post... it only loads the last
"notes"

So, how can I ensure that the value has been received before going on to the
next one?
Thanks for any ideas...


"Mike P" <mi************************@nospam.com> wrote in message
news:Or********************@comcast.com...
Sean,

You are, indeed, checking the readyState before the server has a chance to
reply. You'd probably be better off using xmlhttp.readystatechange to
watch for that reply. Here's some code that should help:

xmlhttp.onreadystatechange = function() {
if ( xmlhttp.readyState == 4 ) {
if ( xmlhttp.status == 200 ) {
doSomeMagic();
} else {
didntWorkOut();
}
}
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
alert(xmlhttp.responseText)
}
}
"Sean Berry" <se**@buildingonline.com> wrote in message
news:1Muaf.4771$UF4.2202@fed1read02...
I have an online store that a customer needs customized by having notes
for each product. I have added a javascript function that sends a session
id, unique product id and some user-defined notes to a script using
XMLHttpRequest. The values get there fine and get stored in a database.

Upon checkout (on the checkout page) I now want to pull all of the data
back from the database and display it. I don't need to display the notes
in order to get them to send, I just want to customer to see their notes
so they don't think they are lost.

So, in my checkout page I have a loop that created divs like the
following
<div id="notes<number>"></div>
where <number> is an incremental number starting from 1.

Then, inside the div I have a 1x1 transparent image with an onload
function call.

This onload function call looks something like this:
onload="getNotes(<number>,<session_id>, <product_id>)
where <number> is the same incremental number from above and <session_id>
and <product_id> are values available inside my loop.

The function getNotes uses XMLHttpConnect to connect to another script
and using the session_id and product_id it retrieves the notes and should
populate the div with the notes.

My problem is this...

The getNotes function get called, then the "onreadystatechange" function
gets called but only the code outside of the check for the "readyState"
and "status" is executed.
The stuff inside of
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { }
never gets called.

Is this because the function getNotes is getting called too quickly after
the first call? If this was the case, I would expect the last one to
work... since it would be the last call.
Is there some way to fix this? Do I need to call the onreadystatechange
function with a setTimeout so that it loops, checking the readyState and
status?

Thanks for any and all help.

Sorry if I did not explain fully.


Nov 3 '05 #3
"Sean Berry" <se**@buildingonline.com> wrote in message
news:gfvaf.4772$UF4.3231@fed1read02...
Now it works as I expected in my first post... it only loads the last
"notes"
So, how can I ensure that the value has been received before going on to >
the next one?


Sean,

I'm not sure I completely follow, but if you're asking how to get to param2,
param3, etc. I would assume that's more a function of what you server's
code is sending. Meaning, it depends on the structure of the XML you're
getting back, and how you're handling it. You may want to treat this as an
object (ala, param["note1"] etc.) or as an array (e.g, param[1], param[2],
etc.).

How is the server structuring the XML? How are you processing it when you
get it back?

Mike
Nov 3 '05 #4


"Mike P" <mi************************@nospam.com> wrote in message
news:gp********************@comcast.com...
"Sean Berry" <se**@buildingonline.com> wrote in message
news:gfvaf.4772$UF4.3231@fed1read02...
Now it works as I expected in my first post... it only loads the last
"notes"
So, how can I ensure that the value has been received before going on to
> the next one?


Sean,

I'm not sure I completely follow, but if you're asking how to get to
param2, param3, etc. I would assume that's more a function of what you
server's code is sending. Meaning, it depends on the structure of the XML
you're getting back, and how you're handling it. You may want to treat
this as an object (ala, param["note1"] etc.) or as an array (e.g,
param[1], param[2], etc.).

How is the server structuring the XML? How are you processing it when you
get it back?

Mike


Mike,

Thanks for the reply.

I figured out the answer to my problem... actually quite simple.
----Problem----
I have a loop that runs and for each item it creates an <img> tag that has
an oncall function. The oncall function connects to a script via
XMLHttpRequest. The problem was that the function was being called the
first time from the first image during the first run in the loop. Then the
function would be called again by the second image created during the second
loop through. So on and so on for each item. Because there was some
latency, once the second call to the function occured it would stop the
first onreadystatechange() and start trying to run it with the for the
second set of parameters. Then, if there was a third, it would stop the
second. So on and so forth. Once it called the function for the last time
(last item, so last run through the loop) it would complete and the
responseText could be used and every thing was super for the last one... all
the others never worked.

----Solution----
In my retrieveNotes() function I have something like the following:

var processing = 0;
function retrieveNotes(value) {
if (processing == 0) {
processing = 1;
create xmlhttp object
set up the connect string
set up onreadystatechange = doSomething;
do connection, etc
} else {
setTimeout("retrieveNotes('" + value + "')", 1000);
}
}

then in my onreadystatechange function

function doSomething() {
if (xmlhttp.readyState == 4 && smlhttp.status == 200) {
do stuff with responseText;
processing = 0;
}
}

So now the retrieveNotes function will either process the request if it is
done processing the request before it goes on to the next one.

If the process is not complete, a setTimeout loop is started and called
until the prior process is complete.

Thanks for the help anyway.

Sean

Nov 4 '05 #5

"Sean Berry" <se**@buildingonline.com> wrote in message
news:1syaf.4805$UF4.3703@fed1read02...
<snip> ----Solution----
In my retrieveNotes() function I have something like the following:

var processing = 0;
function retrieveNotes(value) {
if (processing == 0) {
processing = 1;
create xmlhttp object
set up the connect string
set up onreadystatechange = doSomething;
do connection, etc
} else {
setTimeout("retrieveNotes('" + value + "')", 1000);
}
}

then in my onreadystatechange function

function doSomething() {
if (xmlhttp.readyState == 4 && smlhttp.status == 200) {
do stuff with responseText;
processing = 0;
}
}

So now the retrieveNotes function will either process the request if it is
done processing the request before it goes on to the next one.

If the process is not complete, a setTimeout loop is started and called
until the prior process is complete.

Thanks for the help anyway.

Sean


Glad I could be of help. I do wonder, though, why you're not just
retrieving an array of notes with a single request, then looping through the
array to display each note. That would elminate the need for your
"processing" flag. Also, is it possible that your users may have more or
less than three notes?

Mike
Nov 4 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elizabeth Harmon | last post by:
Good Evening, I am new to the XMLHTTPRequest Object and it's use but i am doing pretty good with it since it's realtively simple? I am having one problem in using a Get method. I am...
20
by: chris.schwalm | last post by:
This is part II of this <a...
1
by: 4levels | last post by:
Dear Folks, I stumbled upon a strange behaviour of the XMLHttpRequest.. Maybe I'm just not well informed enough about its possibilities, so could someone please confirm my question? When I...
7
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
1
by: vocalise | last post by:
The title probably isn't very clear, but I haven't been able to find this problem (or I must be having problems figuring out which search strings to use) so I apologize if this has been addressed...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
25
by: q-rious | last post by:
Hello All, I have a question regarding XMLHttpRequest. I have the following code (as part of a much larger file): function loadXMLDoc(url) { // branch for native XMLHttpRequest object if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.