Avatar of kalmen
kalmen
 asked on

ASP/Ajax application giving "Length Required" error on IIS6/Firefox

I had an application written in ASP (VB Script) with Ajax, and it worked on IIS5 with all browsers. Now, after moving to IIS6, it only works on IE, and gives the error on firefox.

Here is the code:


function getresult(str)
{ 
 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
 
var url="result1.asp";
url=url+"?fname="+document.form1.fname.value+"&lname="+ document.form1.lname.value+"&ext="+ document.form1.ext.value;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(null);
}
 
function goletter(vs){
var nam = this.lt.value;
window.location.href = "letresult.asp?letv=a&letsort="+nam;
 
}
function godept1(URL_List){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url1 = document.form1.dept.options[document.form1.dept.selectedIndex].value;
var space = / /;
var fl=url1.length;
for(f=1;f<fl;f++)
	url1=url1.replace(space, "+");   
url = "result1.asp?deptn="+url1;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(null);
}

Open in new window

ASPAJAXMicrosoft IIS Web Server

Avatar of undefined
Last Comment
kalmen

8/22/2022 - Mon
hielo

When using POST, you need to send the parameters via the send() method, not via the url. You also need to specify a Content-length header that contains the length of the parameters. If you are going to pass the parameters via the url, then you need to use GET instead
function getresult(str)
{ 
 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
 
var url="result1.asp";
var params = "fname="+document.form1.fname.value+"&lname="+ document.form1.lname.value+"&ext="+ document.form1.ext.value;
params=params+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-Length", params.length);
xmlHttp.send(params);
}
 
function goletter(vs){
var nam = this.lt.value;
window.location.href = "letresult.asp?letv=a&letsort="+nam;
 
}
function godept1(URL_List){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url1 = document.form1.dept.options[document.form1.dept.selectedIndex].value;
var space = / /;
var fl=url1.length;
for(f=1;f<fl;f++)
      url1=url1.replace(space, "+");   
url = "result1.asp";
var params = "depth="+url1;
params=params+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.send(params);
}

Open in new window

kalmen

ASKER
I applied the code you gave me and got the same error.  Here is a caption of the HTTP headers from firefox if it helps.
http://localhost/result1.asp?fname=M&lname=&ext=&sid=0.07322916630766085
 
POST /result1.asp?fname=M&lname=&ext=&sid=0.07322916630766085 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost/
Cookie: ASPSESSIONIDCADQASDA=DAPLIFHBEFGEOLHJPMOEIIIF
Pragma: no-cache
Cache-Control: no-cache
 
HTTP/1.x 411 Length Required
Content-Type: text/html
Date: Sun, 20 Jul 2008 13:20:57 GMT
Connection: close
Content-Length: 24

Open in new window

hielo

Change Content-type and Content-length to Content-Type and Content-Length respectively. If problems continue post the rest of your code OR better yet, a url to your page.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
hielo

Wait, you have two function making ajax requests - godept1() and getresult(). Are both giving the same error? On what I posted one of them has Content-Length and the other has Content-length (lowercase L for length), so I would expect at least one of them to work. Whichever one works, change the non-working one accordingly.
kalmen

ASKER
Neither of them are working. I'll run through the code again and possibly paste it here for you. Tomorrow :)

Thanks.
ASKER CERTIFIED SOLUTION
hielo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kalmen

ASKER
This link:
http://www.captain.at/howto-ajax-form-post-request.php
Was very useful, I used the function in it.

I came to realize that FireFox fails to send the content length to IE, which could be a bug?
because when I add: xmlHttp.setRequestHeader("Content-Length", params.length); It doesn't get sent to the server at all.

Would you agree? Of course, I wouldn't have came to this realization without your help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hielo

>>...Would you agree?
I cannot say I do because this makes NO sense:
"I came to realize that FireFox fails to send the content length to IE"

IE and Firefox are both browsers, and both communicate with a Web Server. IE does NOT communicate with Firefox or vice versa. So they are NOT sending anything to each other.
kalmen

ASKER
Ouch... Sorry, I meant to say that firefox doesn't send the content-length to IIS6 (NOT IE, my bad!). After doing research, I came to realize that firefox/ajax/iis6 can only use GET and not POST because of this. Does this make sense to you?
Morcalavin

I used II6 and POST all the time within firefox.   It supports ajax just fine.

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "close"); //keep-alive needed if using ntml authentication

Also, you should be using escapeURIComponent(value) instead of escape(value) when you build our paramenter list.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
hielo

>>After doing research, I came to realize that firefox/ajax/iis6 can only use GET and not POST because of this.
Sorry, still do not agree. IF anything the issue/bug with be with your version of FF. I've implemented ajax/post successfully with no problem with FF 2.x and IE6 posting to IIS6. IF the issue is in fact the browser (as opposed to the server - which I know is not the issue), then at least for the time being you will need to temporatily switch to "GET" until the issue is resolved.
kalmen

ASKER
If you've successfully tested it, then it must work. Let me double check my code. I'll try escapeURIComponent(value) when building my parameter list. I'll keep you posted and thank you for your support.
kalmen

ASKER
You are right. I realized that my code is correct, but when calling the javascript functions I wasn't passing an object, so that is all what was to it.

I was mislead by many posts on the internet that FF is not working with IIS6, and thought this could be the issue. But thanks to your expertise, things are all in shape.

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
hielo

Glad to help, take care.
kalmen

ASKER
Excellent communicator with great knowledge.