XMLHttpRequest question

D

David Lee Lambert

I'm trying to auto-populate later parts of a form based on what's
typed in an earlier part. The following code behaves the way I'd like
in Konqueror and Safari. In Firefox 2.0, it seems like it sets the
status bar with "Searching for...", but never makes the alert() call.
I have the error console open and don't see any errors. I can see in
my web-server logs that it's sending a "200 OK" response to that
request.


window.status = "Searching for vehicle '"+vin+"' in Waybill...";
var win = window;
var req = createXMLHttpRequest();
req.open("GET", "wbVinLookupData.jsp?VIN="+vin, false, null, null);
req.onreadystatechange = function () {
if(req.readyState == 4) {
alert(req.responseText);
var vehicles = [];
try {
vehicles = eval('('+req.responseText+')').vehicles;
} catch (err) {
vehicles = [];
}

Up to that point it works OK in IE 7 as well. Next I act differently
based on the number of returned vehicles:

if (vehicles.length==0) {

alert("VIN '"+vin+"' not found in Waybill!");
} else if (vehicles.length==1) {
var vehicle = vehicles[0];
setVinInfo(win,
vehicle.VIN,
vehicle.customer,
vehicle.location);
win.document.getElementById(
"wbModelInfo").innerHTML = vehicle.modelInfo;
} else {
win.open("chooseVin.jsp?VIN="+vin,
"chooseVin",
"height=300,width=400,scrollbars");
}
}

With IE7, I always get the first ("VIN not found") case, even if I
can see that the returned array has one or several vehicle records.

Does anyone have an idea of the solution to either of these problems?

--
David Lee Lambert
Software Developer, Precision Motor Transport Group, LLC
work phone 517-349-3011 x223
cell phone 586-873-8813
e-mail (e-mail address removed)
 
B

beedeebee

I'm trying to auto-populate later parts of a form based on what's
typed in an earlier part.  The following code behaves the way I'd like
in Konqueror and Safari.  In Firefox 2.0,  it seems like it sets the
status bar with "Searching for...", but never makes the alert() call.
I have the error console open and don't see any errors.  I can see in
my web-server logs that it's sending a "200 OK" response to that
request.

        window.status = "Searching for vehicle '"+vin+"' in Waybill...";
        var win = window;
        var req = createXMLHttpRequest();
        req.open("GET", "wbVinLookupData.jsp?VIN="+vin, false, null, null);
        req.onreadystatechange = function () {
                if(req.readyState == 4) {
                   alert(req.responseText);
                   var vehicles = [];
                   try {
                      vehicles = eval('('+req.responseText+')').vehicles;
                   } catch (err) {
                      vehicles = [];
                   }

Up to that point it works OK in IE 7 as well.  Next I act differently
based on the number of returned vehicles:

                   if (vehicles.length==0) {

                      alert("VIN '"+vin+"' not foundin Waybill!");
                   } else if (vehicles.length==1) {
                      var vehicle = vehicles[0];
                      setVinInfo(win,
                                 vehicle..VIN,
                                 vehicle..customer,
                                 vehicle..location);
                      win.document.getElementById(
                         "wbModelInfo").innerHTML = vehicle.modelInfo;
                   } else {
                      win.open("chooseVin.jsp?VIN="+vin,
                               "chooseVin",
                               "height=300,width=400,scrollbars");
                   }
                }

With IE7,  I always get the first ("VIN not found") case, even if I
can see that the returned array has one or several vehicle records.

Does anyone have an idea of the solution to either of these problems?

--
David Lee Lambert
  Software Developer, Precision Motor Transport Group, LLC
  work phone 517-349-3011 x223
  cell phone 586-873-8813
  e-mail (e-mail address removed)

Is this form all on the same page? If so, I don't think you need ajax
to do this.

You could use the input fields onblur event to call a function that
populated another input field in the form. The onblur function
executes when the control loses focus.
 
D

David Lee Lambert

I'm trying to auto-populate later parts of a form based on what's
typed in an earlier part.  The following code behaves the way I'd like
in Konqueror and Safari.  In Firefox 2.0,  it seems like it sets the
status bar with "Searching for...", but never makes the alert() call. [....]

Is this form all on the same page?  If so, I don't think you need ajax
to do this.

You could use the input fields onblur event to call a function that
populated another input field in the form.  The onblur function
executes when the control loses focus.- Hide quoted text -

I'm trying to build a mostly single-page application; but the first
field needs to be validated against a database that currently has
almost 535 thousand records, and continues to grow. I can't pack that
much data in a hidden field, for instance, and still have the page
load in a reasonable amount of time.

I could abandon the goal of building a Web 2.0-ish application and put
the first field on a separate page; but there are other fields on the
form that should also change some available option sets or simply be
validated as soon as possible. Another concern is that it would be
"clunky" and hard-to-understand.

The code I posted is called from the onblur handler of an input field.

--
David Lee Lambert
Software Developer, Precision Motor Transport Group, LLC
work phone 517-349-3011 x223
cell phone 586-873-8813
e-mail (e-mail address removed)
 
P

pr

David said:
I'm trying to auto-populate later parts of a form based on what's
typed in an earlier part. The following code behaves the way I'd like
in Konqueror and Safari. In Firefox 2.0, it seems like it sets the
status bar with "Searching for...", but never makes the alert() call.
I have the error console open and don't see any errors. I can see in
my web-server logs that it's sending a "200 OK" response to that
request.

Hard one to answer without a working test page to play with, but here goes.
window.status = "Searching for vehicle '"+vin+"' in Waybill...";
var win = window;
var req = createXMLHttpRequest();
req.open("GET", "wbVinLookupData.jsp?VIN="+vin, false, null, null);
req.onreadystatechange = function () {
if(req.readyState == 4) {

If Firefox doesn't get as far as readyState == 4 then what readyState
does it reach?
alert(req.responseText);
var vehicles = [];
try {
vehicles = eval('('+req.responseText+')').vehicles;

We definitely need to see a sample response (the smallest that exhibits
the symptoms):

document.write("<pre>" + req.responseText.replace(/&/g,
"&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</pre>");

and post the result.
} catch (err) {

Until you're finished developing I recommend you leave an alert message
here - you might be trapping some errors you hadn't anticipated.
vehicles = [];
}

Up to that point it works OK in IE 7 as well. Next I act differently
based on the number of returned vehicles:

if (vehicles.length==0) {

alert("VIN '"+vin+"' not found in Waybill!");
} else if (vehicles.length==1) {
var vehicle = vehicles[0];
setVinInfo(win,
vehicle.VIN,
vehicle.customer,
vehicle.location);
win.document.getElementById(
"wbModelInfo").innerHTML = vehicle.modelInfo;
} else {
win.open("chooseVin.jsp?VIN="+vin,
"chooseVin",
"height=300,width=400,scrollbars");
}
}

With IE7, I always get the first ("VIN not found") case, even if I
can see that the returned array has one or several vehicle records.

That too may be a result of your error trap.
Does anyone have an idea of the solution to either of these problems?

Show us the JSON text and you may have more luck. Follow-up to c.l.js.
 
D

David Lee Lambert

If Firefox doesn't get as far as readyState == 4 then what readyState
does it reach?

I fixed this by changing

req2.open("GET", "inspectionTypeData.jsp?
customerId="+customerId,false, null, null);

to

req2.open("GET", "inspectionTypeData.jsp?
customerId="+customerId );

and corresponding changes for the other "open" calls.

For IE, the problem was that I was generating JSON with an extra ","
at the end of each array literal. I switched to a JSON-generating
library and now it works.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,013
Latest member
JewellChes

Latest Threads

Top