We help IT Professionals succeed at work.

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

kalmen
kalmen asked
on
1,326 Views
Last Modified: 2012-08-13
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

Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
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

Author

Commented:
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

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
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.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
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.

Author

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

Thanks.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>...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.

Author

Commented:
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?
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.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>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.

Author

Commented:
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.

Author

Commented:
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.

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
Glad to help, take care.

Author

Commented:
Excellent communicator with great knowledge.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.