Link to home
Create AccountLog in
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

Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

Avatar of kalmen
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

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.
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.
Avatar of 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
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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.
>>...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.
Avatar of 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?
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.
>>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.
Avatar of 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.
Avatar of 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.

Glad to help, take care.
Avatar of kalmen

ASKER

Excellent communicator with great knowledge.